PHP SimpleXMLElement::children() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::children() returns the child elements of an element, so you can loop over them.

Called without arguments, it returns the children that have no namespace. With a namespace URI or prefix, it returns the children that belong to that namespace.

Syntax

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

PHP
$children = $book->children();
CallWhat it does
$book->children()Returns all child elements that have no namespace.
$root->children('urn:x')Returns the children in the namespace with this URI.
$root->children('x', true)Returns the children for the prefix x, because the second argument is true.

A plain foreach over the element itself also loops over its children. Use children() when you need to pick a namespace or want to be explicit.

Library declarations
PHP
SimpleXMLElement::children(?string $namespaceOrPrefix = null, bool $isPrefix = false): ?SimpleXMLElement

Parameters

ParameterDescription
namespaceOrPrefixOptional. A namespace URI or prefix. The default is null, which means no namespace.
isPrefixOptional. Set to true when the first argument is a prefix and not a URI. The default is false.

Return Value

Return ValueDescription
SimpleXMLElementAn object that holds the child elements. It is empty when there are no children.
nullReturned when called on an attribute node.

PHP Examples

Example 1 — Loop Over Child Elements

Print the name and text of every child of the book element.

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

  1. The book element has two children, title and author.
  2. children() returns an object that holds both of them.
  3. foreach visits them in document order.
  4. getName() gives the element name and the cast gives its text.

Example 2 — An Element Without Children

When an element holds only text, children() returns an empty object. count() shows how many there are.

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

echo count($book->children()), PHP_EOL;
echo count($book->title->children()), PHP_EOL;

How It Works

  1. The book element has one child, the title element.
  2. count($book->children()) returns 1.
  3. The title element contains only text, so it has no children.
  4. count($book->title->children()) returns 0.

Example 3 — Read Children from a Namespace

The same XML has an item in the x namespace and an item with no namespace. children() lets you choose which one to read.

PHP
<?php
$xml = '<root xmlns:x="urn:x"><x:item>Tea</x:item><item>Milk</item></root>';
$root = simplexml_load_string($xml);

echo (string) $root->children('urn:x')->item, PHP_EOL;
echo (string) $root->children('x', true)->item, PHP_EOL;
echo (string) $root->item, PHP_EOL;

How It Works

  1. The first item uses the x prefix, which stands for the namespace urn:x.
  2. children('urn:x') selects the children by namespace URI, so item is Tea.
  3. children('x', true) selects them by prefix and gives the same element.
  4. $root->item with no call looks only at the no-namespace children, so it is Milk.

Frequently Asked Questions

It returns the child elements of the element. You can loop over the result with foreach or read a child by name.
Not always. foreach ($book as $child) also works. children() is useful when you want to be clear, or when you need to choose a namespace.
Call children() with the namespace URI, or with the prefix and true as the second argument. Then read the element from the result.
Use count($element->children()) or $element->count(). Both return the number of child elements.
children() returns the child elements, while attributes() returns the attributes of the element. Both accept a namespace.

Next: SimpleXMLElement::count()

SimpleXMLElement::count() returns the number of child elements of an element.

SimpleXMLElement::count() 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