Latest web development tutorials

PHP count () function

PHP SimpleXML Reference Manual PHP SimpleXML Reference Manual

Examples

Calculate the number of car node's children:

<?php
$xml=<<<XML
<cars>
<car name="Volvo">
<child/>
<child/>
<child/>
<child/>
</car>
<car name="BMW">
<child/>
<child/>
</car>
</cars>
XML;

$elem=new SimpleXMLElement($xml);
foreach ($elem as $car)
{
printf("%s has %d children.<br>", $car['name'], $car->count());
}
?>

Running instance »

Definition and Usage

count () function to calculate the number of the specified node's child nodes.


grammar

count();

technical details

return value: Returns the number of child nodes of an element.
PHP version: 5.3+


PHP SimpleXML Reference Manual PHP SimpleXML Reference Manual