Content · Programming · PHP

PHP simplexml_load_string() Function: Usage and Examples

Parses trusted or validated XML markup from a string into a SimpleXML tree. Learn how to apply it with examples and common mistakes.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

A common use for simplexml_load_string is processing data without rebuilding the operation manually. Parses trusted or validated XML markup from a string into a SimpleXML tree.

What Is simplexml_load_string in PHP?


Parses trusted or validated XML markup from a string into a SimpleXML tree.

Syntax:

simplexml_load_string($data, $className, $options, $namespaceOrPrefix, $isPrefix)

Parameters and Return Value


  • $data — The input text or XML content.
  • $className — Optional class name used for the SimpleXML wrapper.
  • $options — Bitmask or flags that configure the operation.
  • $namespaceOrPrefix — Namespace URI or prefix used in the SimpleXML selection.
  • $isPrefix — Whether the namespace argument represents a prefix.

Check the returned value, including false when applicable, or handle exceptions for invalid input. The required XML/DOM extension must be installed.

Example 1: Basic Usage


This example demonstrates the main operation with a small, controlled input.

<?php

$xmlItems = simplexml_load_string("<root><item>Guide</item></root>", SimpleXMLElement::class, LIBXML_NONET);
if ($xmlItems === false) { throw new RuntimeException("Invalid XML"); }
echo (string)$xmlItems->item;

The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.

Example 2: Another Input


This example changes the input or surrounding setup to illustrate another behavior.

<?php

$xmlItems = simplexml_load_string("<root><item>Guide</item></root>", SimpleXMLElement::class, LIBXML_NONET);
if ($xmlItems === false) { throw new RuntimeException("Invalid XML"); }
echo (string)$xmlItems->item;
// Validate the result in a condition appropriate to this operation.
if (function_exists("json_encode")) { echo "Operation demonstrated"; }

The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.

Practical Example


This application-style example shows a self-contained usage pattern and checks its result.

<?php

$xmlItems = simplexml_load_string("<root><item>Guide</item></root>", SimpleXMLElement::class, LIBXML_NONET);
if ($xmlItems === false) { throw new RuntimeException("Invalid XML"); }
echo (string)$xmlItems->item;
// Inspect the surrounding runtime requirements before adapting this example.
echo PHP_VERSION;

The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.

Important Behavior and Edge Cases


  • Check invalid input, encoding requirements and return values. Some operations depend on optional PHP extensions or runtime configuration.
  • Use the intended input type and handle empty or malformed values explicitly.
  • Where this API requires an extension, version, or file, document that dependency before using it in a real application.

Comparison With Related Functions


Function or MethodWhen to Use It
simplexml_load_stringParses trusted or validated XML markup from a string into a SimpleXML tree.
DOMDocument::loadXMLParses XML markup from a string.

Common Beginner Mistakes


MistakeCorrect Approach
Using this API without checking the required input or extensionCheck invalid input, encoding requirements and return values. Some operations depend on optional PHP extensions or runtime configuration.
Treating malformed input as a successful resultHandle invalid values and documented failure results explicitly.
Using this operation for the wrong data representationChoose the matching data format and compare its result to the required output.

Key Takeaways


  • Parses trusted or validated XML markup from a string into a SimpleXML tree.
  • Check invalid input, encoding requirements and return values. Some operations depend on optional PHP extensions or runtime configuration.
  • Test both the expected case and at least one failure or empty-input case before deployment.

Parses trusted or validated XML markup from a string into a SimpleXML tree. Use it when the operation fits your input, output and validation requirements.

Share this resourceSend this page to someone who may find it useful.