0
0
PHPprogramming~10 mins

Generator return values in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generator return values
Start Generator Function
Yield values one by one
Generator finishes
Return value captured
Access return value via getReturn()
End
The generator yields values step-by-step, then returns a final value which can be accessed after iteration ends.
Execution Sample
PHP
<?php
function gen() {
  yield 1;
  yield 2;
  return 3;
}
$g = gen();
foreach ($g as $val) {
  echo $val . "\n";
}
echo "Return: " . $g->getReturn() . "\n";
?>
This code yields two values from the generator, then returns a final value accessible after iteration.
Execution Table
StepActionYielded ValueGenerator StateOutput
1Start generator and yield first value1RunningPrint 1
2Yield second value2RunningPrint 2
3Generator reaches return statementNoneFinishedNo yield, iteration ends
4Access return value via getReturn()3FinishedPrint Return: 3
💡 Generator finished after return statement, iteration stops, return value accessed separately
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$valundefined12undefinedundefined
$g->getReturn()undefinedundefinedundefined33
Key Moments - 2 Insights
Why does the return value not appear in the foreach loop output?
The foreach loop only outputs values yielded by yield statements (see steps 1 and 2). The return value is not yielded, so it appears only when accessed with getReturn() after iteration ends (step 4).
What happens when the generator hits the return statement?
At step 3, the generator finishes and stops yielding values. The return value is stored internally and can be retrieved later with getReturn(), but it does not produce output during iteration.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the generator state after yielding the second value?
ARunning
BFinished
CPaused
DNot started
💡 Hint
Check the 'Generator State' column at Step 2 in the execution_table.
At which step does the generator stop yielding values?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step where 'Generator State' changes to 'Finished' and no value is yielded.
If the return statement was removed, what would happen to the output at step 4?
AIt would print the last yielded value
BIt would print nothing or null
CIt would print 0
DIt would cause an error
💡 Hint
Without a return, getReturn() returns null or nothing after iteration ends.
Concept Snapshot
PHP Generators yield values stepwise using yield.
When generator ends, it can return a value with return.
Return value is NOT yielded, so foreach won't show it.
Use getReturn() on generator after iteration to get it.
Useful to send final result after yielding sequence.
Full Transcript
This example shows a PHP generator function that yields two values, 1 and 2, then returns 3. The foreach loop prints the yielded values 1 and 2. After the loop finishes, the generator is done, and the return value 3 is accessed using getReturn(). The return value does not appear during iteration because only yield produces output. The generator state changes from running to finished when the return statement is reached. This allows the program to get a final result from the generator after all values have been yielded.