Content · Web Development · PHP

PHP iterator_apply() Function: Usage and Examples

PHP iterator_apply() Function: Usage and Examples: Calls a callback for every element of a Traversable until the callback returns false; the callback must accept any supplied arguments.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

The feature illustrated below operates on PHP values, objects, or language metadata.

What Is iterator_apply in PHP?


Calls a callback for every element of a Traversable until the callback returns false; the callback must accept any supplied arguments.

iterator_apply(Traversable $iterator, callable $callback, ?array $args = ...) : int

Parameters:

  • $iterator — Required. The iterator to attach or traverse.
  • $callback — Required. The callback argument used to configure this operation; its accepted values follow the declared PHP type.
  • $args — Optional. The args argument used to configure this operation; its accepted values follow the declared PHP type.

Return type: int. The callback must return true to continue iteration.

Example 1: Understanding the Basic Behavior


<?php

$iterator = new ArrayIterator([1,2,3]);
$sum = 0;
iterator_apply($iterator, static function () use (&$sum, $iterator): bool { $sum += $iterator->current(); return true; });
echo $sum;

Output:

6

Explanation: Calls a callback for every element of a Traversable until the callback returns false; the callback must accept any supplied arguments.

Example 2: Testing Another Case


<?php

$iterator = new ArrayIterator(['a','b','c']);
$count = iterator_apply($iterator, static fn(): bool => true);
echo $count;

Output:

3

Explanation: The callback must return true to continue iteration.

Practical Example: Applying the Operation


<?php

$iterator = new ArrayIterator([2,4,6]);
$seen = [];
iterator_apply($iterator, static function () use ($iterator, &$seen): bool { $seen[] = $iterator->current() * 2; return true; });
print_r($seen);

Output:

Array
(
    [0] => 4
    [1] => 8
    [2] => 12
)

Explanation: The callback arguments come from the third parameter, not automatically from current iterator values.

Important Behavior and Edge Cases


  • The callback must return true to continue iteration.
  • The callback arguments come from the third parameter, not automatically from current iterator values.

Comparison With Related Approaches


ApproachWhen to Use It
iterator_applyCalls a callback for every element of a Traversable until the callback returns false; the callback must accept any supplied arguments.
foreachiterator_apply() runs a callback while it returns true; foreach gives direct control over each element and its key.

Common Beginner Mistakes


MistakeCorrect Approach
Forgetting that the callback must return true for iteration to continue.The callback must return true to continue iteration.
Choosing foreach without checking whether it provides the same result.iterator_apply() runs a callback while it returns true; foreach gives direct control over each element and its key.

Key Takeaways


  • Calls a callback for every element of a Traversable until the callback returns false; the callback must accept any supplied arguments.
  • The callback must return true to continue iteration.
  • The callback arguments come from the third parameter, not automatically from current iterator values.

The examples demonstrate where iterator_apply fits within php programming. Verify the relevant preconditions whenever you adapt the code to real data.

Share this resourceSend this page to someone who may find it useful.