Example 1 — Add a Child Element
Add an author element to a book and read it back.
<?php
$book = simplexml_load_string('<book><title id="1">PHP</title></book>');
$book->addChild('author', 'Ann');
echo (string) $book->author, PHP_EOL;
echo $book->count(), PHP_EOL; How It Works
- The book element starts with only a title child.
- addChild('author', 'Ann') adds a new author element with the text Ann.
- (string) $book->author reads the text back.
- count() now returns 2 because the book has two children.

