PHP count() Function

❮ PHP SimpleXML Reference

Example

Count the children of the <car> element:

<?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

SimpleXMLElement::count();

Technical Details

Return Value: The number of child elements of an element
PHP Version: 5.3+

❮ PHP SimpleXML Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.