Example 1 — Cast an Element to a String
Read the title text with a cast and with a direct method call. Both give the same result.
<?php
$book = simplexml_load_string('<book><title id="1">PHP</title></book>');
echo (string) $book->title, PHP_EOL;
echo $book->title->__toString(), PHP_EOL; How It Works
- simplexml_load_string() parses the XML and returns the root element.
- $book->title is a SimpleXMLElement object, not a plain string.
- The (string) cast makes PHP call __toString() for you.
- Calling __toString() by hand returns exactly the same text.

