Latest web development tutorials
 

PHP count() Function

< PHP SimpleXML Reference

Example

Count the children of the car nodes:

<?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());
  }
?>
Run example »

Definition and Usage

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


Syntax

count();

Technical Details

Return Value: Returns the number of children of an element
PHP Version: 5.3+

< PHP SimpleXML Reference