Produce PHP Values Only When foreach Requests Them
See when yield produces PHP values, how foreach requests them, and what an early break skips compared with building an array first.
If you need to visit values without building a complete array first, put yield inside a function and iterate over the returned Generator with foreach. Calling the function gives you the generator object; iteration requests its values as needed. That timing matters when a loop stops early: the loop does not request later values. An array-building loop makes a different choice, adding every value before a subsequent foreach visits any of them.
Put yield in the function
Any function containing yield is a generator function, according to the PHP generator syntax manual. The counted function below can produce the numbers 1 through 3. Both generator passes call it with the same limit, so their different output comes from where iteration stops, not from different inputs.
The produce marker sits immediately before yield. It shows when execution reaches each value, rather than merely showing what eventually appears in the loop. The consume marker sits inside foreach. Reading the two together shows which part of the file is active at each step: the generator produces a value, then the loop handles that value.
Store the Generator, then consume a full pass
This is a complete PHP file. It includes a full generator pass, a separate pass that stops at 2, and an array-building counterpart. The markers use newline characters so their order is easy to follow as text.
<?php
function countedValues($limit)
{
for ($value = 1; $value <= $limit; $value++) {
echo "produce $value\n";
yield $value;
}
}
echo "Generator, full pass:\n";
$generated = countedValues(3);
echo ($generated instanceof Generator ? "Stored a Generator\n" : "Unexpected result\n");
foreach ($generated as $value) {
echo "consume $value\n";
}
echo "Generator, stop at 2:\n";
$shortPass = countedValues(3);
foreach ($shortPass as $value) {
echo "consume $value\n";
if ($value === 2) {
break;
}
}
echo "Array, build first:\n";
$items = array();
for ($value = 1; $value <= 3; $value++) {
$items[] = $value;
echo "add $value\n";
}
echo "Array, visit:\n";
foreach ($items as $value) {
echo "consume $value\n";
}
For this file, the expected output is:
Generator, full pass:
Stored a Generator
produce 1
consume 1
produce 2
consume 2
produce 3
consume 3
Generator, stop at 2:
produce 1
consume 1
produce 2
consume 2
Array, build first:
add 1
add 2
add 3
Array, visit:
consume 1
consume 2
consume 3
Notice what is absent between Stored a Generator and produce 1: storing the result of countedValues(3) has not printed a production marker. The Generator class documentation says generator objects are returned from generators. It describes starting one as executing it through its first yield, and its next operation as resuming execution. In the full pass, each produce line therefore appears before the matching consume line, not alongside all the other production lines at the time of the call.
Stop at 2, then compare the array loop
The second pass calls countedValues(3) again, then breaks after consuming 2. There is no produce 3 marker in that pass: foreach never requests that value. The placement of the marker makes the observation precise. The statement that would print produce 3 sits immediately before the third yield, and this pass does not reach it. The example shows which statements are reached before the break; it does not measure execution time or make a broader claim about every kind of work a generator might perform.
The array counterpart makes its order equally explicit. Its building loop prints add 1, add 2, and add 3 before the Array, visit marker or any array-consumption marker. Even if a later visit stopped after 2, these three additions would already have happened. In this small example, choose the generator when the visiting loop might stop before requesting every value; choose to build the array first when having all three values before the visit is the intended flow. The markers show the difference in when these values are added or produced, without requiring a larger comparison of arrays and generators.
Call the function again for a fresh pass
The full pass and short pass use separate generator-function calls. That separation matters if you want to start at 1 again: the PHP manual’s generator comparison describes generators as forward-only and says a generator must be rebuilt by calling its function again to iterate over its values another time. Do not treat the stored Generator like the array in this example, which the code can visit from its beginning with another foreach. For a fresh generator pass, call countedValues(3) again and iterate over the new result.
