PHP SimpleXMLElement::count() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::count() returns the number of child elements of an element.

It counts only the direct children, not deeper levels. You can call it as a method, or use the count() function on the element, because SimpleXMLElement supports both.

Syntax

Call SimpleXMLElement::count() with the values you need. Start with this line.

PHP
$total = $list->count();
CallWhat it does
$list->count()Returns the number of direct child elements of $list.
count($list)The count() function gives the same result for a SimpleXMLElement.
$list->item->count()Counts the children of the first item element.

Only direct children are counted. Elements nested deeper, and attributes, are not part of the total.

Library declarations
PHP
SimpleXMLElement::count(): int

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
intThe number of direct child elements. It is 0 when there are no children.

PHP Examples

Example 1 — Count the Items in a List

Count the child elements of a list with both the method and the function.

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

  1. The list element has three item children.
  2. $list->count() returns 3.
  3. The count() function works on a SimpleXMLElement too.
  4. Both lines print the same number.

Example 2 — Only Direct Children Are Counted

Nested elements are not part of the total. Count each level on its own.

PHP
<?php
$shop = simplexml_load_string('<shop><drinks><item>Tea</item><item>Milk</item></drinks></shop>');

echo "Shop: ", $shop->count(), PHP_EOL;
echo "Drinks: ", $shop->drinks->count(), PHP_EOL;

How It Works

  1. The shop element has only one direct child, drinks.
  2. $shop->count() returns 1, even though there are two items deeper down.
  3. $shop->drinks->count() looks one level deeper.
  4. The drinks element has two item children, so it returns 2.

Example 3 — Watch the Count Change

Start with an empty element, add a child, and check the count before and after.

PHP
<?php
$list = simplexml_load_string('<list/>');

echo "Before: ", $list->count(), PHP_EOL;

$list->addChild('item', 'Tea');

echo "After: ", $list->count(), PHP_EOL;

How It Works

  1. <list/> is an empty element, so it has no children.
  2. count() returns 0 for it.
  3. addChild() adds one item element.
  4. The second count() call returns 1.

Frequently Asked Questions

It returns the number of direct child elements of the element. It returns 0 when the element has no children.
Yes. SimpleXMLElement implements the Countable interface, so both ways return the same number.
No. It counts only the direct children. To count deeper elements, call count() on the child element.
No. Attributes are not children. To count attributes, use count($element->attributes()).
Use count($parent->item). This counts only the child elements named item.

Next: SimpleXMLElement::getDocNamespaces()

SimpleXMLElement::getDocNamespaces() returns the namespaces that are declared in an XML do.

SimpleXMLElement::getDocNamespaces() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful