Example 1 — Count the Items in a List
Count the child elements of a list with both the method and the function.
<?php
$list = simplexml_load_string('<list><item>Tea</item><item>Milk</item><item>Juice</item></list>');
echo $list->count(), PHP_EOL;
echo count($list), PHP_EOL; How It Works
- The list element has three item children.
- $list->count() returns 3.
- The count() function works on a SimpleXMLElement too.
- Both lines print the same number.

