Example 1 — Loop Over Child Elements
Print the name and text of every child of the book element.
<?php
$book = simplexml_load_string('<book><title>PHP</title><author>Ann</author></book>');
foreach ($book->children() as $child) {
echo $child->getName(), ": ", (string) $child, PHP_EOL;
} How It Works
- The book element has two children, title and author.
- children() returns an object that holds both of them.
- foreach visits them in document order.
- getName() gives the element name and the cast gives its text.

