PHP SimpleXMLElement::hasChildren() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::hasChildren() checks whether the element that the iterator currently points to has child elements.

It is part of the RecursiveIterator interface that SimpleXMLElement implements in PHP 8. You use it together with getChildren() when you walk through nested XML yourself. To simply count children, use count($xml->children()) instead.

Syntax

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

PHP
$xml->hasChildren();
CallWhat it does
$xml->hasChildren()Returns true when the current child element has child elements of its own.
$xml->getChildren()Returns the current element so you can loop over its children.
count($xml->children())Counts the child elements of an element, without using the iterator.

hasChildren() looks at the current child of the object, not at the object itself. Call rewind() first and check valid(). An element that holds only text counts as having no children.

Library declarations
PHP
SimpleXMLElement::hasChildren(): bool

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
booltrue when the current child element contains at least one child element, otherwise false.

PHP Examples

Example 1 — Check Each Child

Move through the children of library and ask whether each one has children of its own.

PHP
<?php
$xml = simplexml_load_string(
    '<library><book><title>PHP</title></book><note>Hello</note></library>'
);

$xml->rewind();
echo $xml->key(), ': ', var_export($xml->hasChildren(), true), PHP_EOL;

$xml->next();
echo $xml->key(), ': ', var_export($xml->hasChildren(), true), PHP_EOL;

How It Works

  1. The library root has two children: book, which holds a title element, and note, which holds only text.
  2. After rewind(), the iterator points to book, and hasChildren() is true because of the title element.
  3. next() moves the iterator to note.
  4. hasChildren() is false for note, because text is not a child element.

Example 2 — Branches and Leaves

Loop over the children and label each as a branch (it has children) or a leaf (it does not). Attributes do not count.

PHP
<?php
$xml = simplexml_load_string(
    '<shop>'
    . '<item id="1">Pen</item>'
    . '<group><item>Book</item><item>Bag</item></group>'
    . '<empty/>'
    . '</shop>'
);

for ($xml->rewind(); $xml->valid(); $xml->next()) {
    $label = $xml->hasChildren() ? 'branch' : 'leaf';
    echo $xml->key(), ' is a ', $label, PHP_EOL;
}

How It Works

  1. The shop element has three children: item, group, and empty.
  2. item holds only text and an id attribute, so hasChildren() is false and it is a leaf.
  3. group holds two item elements, so hasChildren() is true and it is a branch.
  4. empty has nothing inside, so it is a leaf too.

Example 3 — Count Children Without the Iterator

When you only want to know whether an element has children, children() and count() are simpler than the iterator methods.

PHP
<?php
$xml = simplexml_load_string(
    '<library><book><title>PHP</title><author>Sam</author></book><note>Hello</note></library>'
);

echo 'Library children: ', count($xml->children()), PHP_EOL;
echo 'Book children: ', count($xml->book->children()), PHP_EOL;
echo 'Note children: ', count($xml->note->children()), PHP_EOL;

foreach ($xml as $name => $child) {
    echo $name, ': ', count($child->children()) > 0 ? 'yes' : 'no', PHP_EOL;
}

How It Works

  1. children() returns the child elements of an element, and count() counts them.
  2. library has 2 children, the book has 2 children, and the note has 0 children.
  3. foreach visits book and note without any manual iterator calls.
  4. Checking count($child->children()) > 0 gives the same answer that hasChildren() would give.

Frequently Asked Questions

It tells you whether the element at the current iterator position has child elements. It returns true or false.
No. Only child elements count, so an element with just text or just attributes returns false.
No. It checks the current child of that object, so you need rewind() and valid() first. To check an element directly, use count($element->children()) > 0.
Use count($xml->children()). It returns the number of child elements and needs no rewind() or next() calls.
Rarely. It is mostly for recursive walking of XML trees. Most code uses foreach and children() instead.

Next: SimpleXMLElement::key()

SimpleXMLElement::key() returns the name of the child element that the iterator currently .

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