Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

iterator_count()

Count every element in a traversal.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

iterator_count(Traversable|array $iterator): int

Count every element in a traversal. This can consume a generator and the iterator position is not guaranteed to be preserved. Since PHP 8.2 an array is accepted directly. For a one-pass input, count as you process it if the values are still needed.

Signature

iterator_count(Traversable|array $iterator): int

Example output

[2,false,3]

Examples

<?php
function stream(): Generator { yield "a"; yield "b"; }
$it = stream();
echo json_encode([iterator_count($it),$it->valid(),iterator_count([3,4,5])]);

Result

[2,false,3]

Sources