PHP Tutorial - PHP SimpleXML count() Function






The count() function counts the children of a specified node.

Syntax

PHP count() Function has the following syntax.

count();

Return

Returns the number of children of an element

Example

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.