PHP SimpleXMLElement::addChild() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::addChild() adds a new child element to an XML element and returns that new child.

It is the main way to build an XML document in PHP code. Because the new child is returned, you can add attributes or more children to it right away.

Syntax

Call SimpleXMLElement::addChild() with the values you need. Start with this line.

PHP
$author = $book->addChild('author', 'Ann');
CallWhat it does
$book->addChild('author', 'Ann')Adds an author element with the text Ann.
$book->addChild('summary')Adds an empty summary element, because no value is given.
$book->addChild('x:note', 'Hi', 'urn:x')Adds a namespaced child by passing the namespace URI.

The new child is added after the existing children. Store the return value if you want to keep working on the new element.

Library declarations
PHP
SimpleXMLElement::addChild(string $qualifiedName, ?string $value = null, ?string $namespace = null): ?SimpleXMLElement

Parameters

ParameterDescription
qualifiedNameRequired. The name of the new child element.
valueOptional. The text content of the child. The default is null, which creates an empty element.
namespaceOptional. The namespace URI of the child. The default is null.

Return Value

Return ValueDescription
SimpleXMLElementThe new child element, so you can keep adding attributes or children.
nullReturned if the child cannot be added.

PHP Examples

Example 1 — Add a Child Element

Add an author element to a book and read it back.

PHP
<?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

  1. The book element starts with only a title child.
  2. addChild('author', 'Ann') adds a new author element with the text Ann.
  3. (string) $book->author reads the text back.
  4. count() now returns 2 because the book has two children.

Example 2 — Empty Elements and Chained Changes

Leave out the value to create an empty element. Use the returned child to add an attribute straight away.

PHP
<?php
$book = simplexml_load_string('<book><title id="1">PHP</title></book>');

$book->addChild('summary');
$price = $book->addChild('price', '9.50');
$price->addAttribute('currency', 'USD');

echo $book->summary->asXML(), PHP_EOL;
echo $price->asXML(), PHP_EOL;

How It Works

  1. addChild('summary') has no value, so an empty element is created.
  2. The second addChild() call returns the new price element, stored in $price.
  3. addAttribute() is called on that returned element to set the currency.
  4. asXML() shows that an empty element is written as <summary/>.

Example 3 — Build a List in a Loop

Start with an empty list element and add one item for each value in an array.

PHP
<?php
$list = simplexml_load_string('<list></list>');

foreach (['Tea', 'Milk', 'Juice'] as $drink) {
    $list->addChild('item', $drink);
}

echo "Items: ", $list->count(), PHP_EOL;

foreach ($list->item as $item) {
    echo (string) $item, PHP_EOL;
}

How It Works

  1. The list starts empty, with no children.
  2. The first loop calls addChild() once for each drink in the array.
  3. count() reports 3 children after the loop.
  4. The second loop prints the text of every item.

Frequently Asked Questions

It adds a new child element to the element you call it on and returns the new child. You can pass a text value as the second argument.
It returns the new child as a SimpleXMLElement. Keep it in a variable when you want to add attributes or more children to it.
Call addChild() with only the name. The element is written as <name/> when you print the XML.
At the end. The new element comes after all the existing children.
Escape the text with htmlspecialchars() before you pass it. This keeps characters like & and < from breaking the XML.

Next: SimpleXMLElement::asXML()

SimpleXMLElement::asXML() turns a SimpleXMLElement back into an XML string, or saves it to.

SimpleXMLElement::asXML() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful