haschildren()
Learn how PHP's SimpleXMLIterator::hasChildren() reports whether the iterator's current XML element has child nodes, with runnable examples and pitfalls.
What hasChildren() does
hasChildren() is a method of the SimpleXMLIterator class. It returns true when the element the iterator is currently positioned on has at least one child element, and false when that element is a leaf (text-only or empty) node.
It comes from PHP's RecursiveIterator interface, which both SimpleXMLIterator and SimpleXMLElement implement. Its job is to tell a recursive traversal engine, "should I descend into this node?" That is the key idea, and the source of most confusion:
hasChildren() does not ask "does $this have children?" It asks "does the element at the iterator's current position have children?" You normally call it while looping with rewind() / valid() / next(), or let a RecursiveIteratorIterator call it for you — not directly on an arbitrary element.
public SimpleXMLIterator::hasChildren(): boolThe method takes no parameters and returns a bool. To read the children once it returns true, pair it with getChildren().
Setting up a SimpleXMLIterator
hasChildren() only exists on SimpleXMLIterator, so create one from your XML string with new SimpleXMLIterator(), or load a document and cast it. Here is a small catalog where one element has children and one does not:
<?php
$xml = new SimpleXMLIterator(<<<XML
<store>
<book>
<title>Modern PHP</title>
<author>Josh Lockhart</author>
</book>
<note>Closed on holidays</note>
</store>
XML);
for ($xml->rewind(); $xml->valid(); $xml->next()) {
if ($xml->hasChildren()) {
echo $xml->key() . " has children:\n";
foreach ($xml->getChildren() as $name => $value) {
echo " {$name}: {$value}\n";
}
} else {
echo $xml->key() . " (leaf): " . $xml->current() . "\n";
}
}Output:
book has children:
title: Modern PHP
author: Josh Lockhart
note (leaf): Closed on holidaysNotice the loop walks the iterator manually with rewind(), valid(), next(), key() and current(). hasChildren() reports on whichever element the cursor is on at that moment.
Walking the whole tree recursively
The real payoff of hasChildren() shows up when you hand the iterator to a RecursiveIteratorIterator. That wrapper calls hasChildren() and getChildren() for you, descending automatically so you can flatten a nested document of any depth:
<?php
$xml = new SimpleXMLIterator(<<<XML
<library>
<shelf>
<book>
<title>PHP Basics</title>
</book>
</shelf>
<desk>Front entrance</desk>
</library>
XML);
$tree = new RecursiveIteratorIterator(
$xml,
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($tree as $name => $node) {
echo str_repeat(' ', $tree->getDepth()) . $name . "\n";
}Output:
shelf
book
title
deskYou never call hasChildren() by hand here — RecursiveIteratorIterator uses it internally to decide when to recurse.
When to use it
- You are iterating an unknown XML structure and need to know whether to drill deeper before reading values.
- You are building a tree view, breadcrumb, or flattened list from nested XML.
- You want PHP's iterator machinery (
RecursiveIteratorIterator, filters) to traverse XML for you instead of writing nestedforeachloops.
If you simply want the children of a known element, you usually don't need hasChildren() at all — call children() on a SimpleXMLElement and check whether the result is empty with count().
Common pitfalls
- Calling it on
SimpleXMLElement. A plainSimpleXMLElementcreated withsimplexml_load_file()orsimplexml_load_string()implementsRecursiveIterator, but the friendlyhasChildren()semantics belong toSimpleXMLIterator. Use that class when you want this method. - Expecting it to detect attributes or text.
hasChildren()looks only at child elements. An element holding only text or only attributes returnsfalse. - Calling it before positioning the cursor. Always
rewind()(or iterate) first; the result reflects the current position, which is undefined before the first element.
Conclusion
SimpleXMLIterator::hasChildren() is the gatekeeper of recursive XML traversal: it reports whether the iterator's current element has child elements so your code — or a RecursiveIteratorIterator — knows when to descend. Pair it with getChildren() to read those children, and reach for children() or the wider SimpleXML guide when you just need a node's contents directly.