W3docs

SimpleXml to string

To convert a SimpleXml object to a string in PHP, you can use the asXML() function.

To convert a SimpleXml object to a string in PHP, you can use the asXML() function. For example:

Example of using the asXML() function to convert a SimpleXml object to a string in PHP

php— editable, runs on the server

This will convert the $xml object to a string with the contents <example>Hello World</example>.

Alternatively, you can cast the object to a string using (string) $xml. This extracts the text content of the root element.

Example of casting a SimpleXml object to a string in PHP

php— editable, runs on the server

Note that (string) $xml returns only the text content (Hello World), whereas $xml->asXML() returns the complete XML structure.