The count() function counts the children of a specified node.
PHP count() Function has the following syntax.
count();
Returns the number of children of an element
Count the children of the book nodes:
<?php// w ww . ja v a2s.c o m
$xml=<<<XML
<books>
<book name="PHP">
<child/>
</book>
<book name="Java">
<child/>
<child/>
</book>
</books>
XML;
$elem=new SimpleXMLElement($xml);
foreach ($elem as $book){
printf("%s has %d children.\n", $book['name'], $book->count());
}
?>
The code above generates the following result.