PHP SimpleXML count() Function

Description

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//  ww  w  .j a  v a2 s.  co 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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions