iterator_count()

iterator_count() — Count the elements in an iterator.

Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.1.0, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Count the elements in an iterator. iterator_count() is not guaranteed to retain the current position of the iterator.

Signature

iterator_count(Traversable|array $iterator): int

Parameters

ParameterTypeWhat it takes
$iteratorTraversable|arrayThe iterator being counted.

Return value

The number of elements in iterator.

Example

A runnable snippet:

$iterator = new ArrayIterator(array('recipe'=>'pancakes', 'egg', 'milk', 'flour')); var_dump(iterator_count($iterator));

Output:

int(4)

Changelog

VersionChange
8.2.0The type of iterator has been widened from Traversable to Traversable|array.

All PHP 8.5 built-in functions

Reference card built from the PHP manual page iterator_count() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.