0
0
PHPprogramming~10 mins

Generator function execution model in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generator function execution model
Call generator function
First iteration: Start execution until yield
Yield value to caller
Pause execution
Caller resumes generator
Continue execution until next yield or return
Repeat yield or finish
Generator ends, return value or stop iteration
This flow shows how a PHP generator function runs: it starts, yields values one by one pausing after each, and resumes when the caller asks for the next value until it finishes.
Execution Sample
PHP
<?php
function countToThree() {
  yield 1;
  yield 2;
  yield 3;
}
$gen = countToThree();
foreach ($gen as $value) {
  echo $value . "\n";
}
This code defines a generator that yields numbers 1 to 3, then prints each number one by one.
Execution Table
StepActionGenerator StateYielded ValueOutput
1Call countToThree()Created, before first yieldnonenone
2First yield reachedPaused after yielding 111
3Resume generatorRunning, before second yieldnone1
4Second yield reachedPaused after yielding 222
5Resume generatorRunning, before third yieldnone2
6Third yield reachedPaused after yielding 333
7Resume generatorNo more yields, generator endsnone3
8Iteration endsGenerator finishednone3
💡 Generator ends after yielding all values; foreach loop stops.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
$genGenerator object createdPaused at yield 1Paused at yield 2Paused at yield 3Finished
Key Moments - 2 Insights
Why does the generator pause after each yield instead of running all at once?
Because each yield sends a value back to the caller and pauses execution, waiting for the caller to request the next value (see steps 2, 4, 6 in execution_table).
What happens when the generator finishes all yields?
The generator ends and the foreach loop stops iterating (see step 7 and 8 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the generator state after the first yield?
AFinished
BRunning before first yield
CPaused after yielding 1
DNot started
💡 Hint
Check the 'Generator State' column at Step 2 in the execution_table.
At which step does the generator finish all yields and end?
AStep 4
BStep 7
CStep 6
DStep 8
💡 Hint
Look for 'No more yields, generator ends' in the 'Action' column.
If we add another yield after yield 3, how would the variable tracker change after Step 6?
AIt would show 'Paused at new yield 4'
BIt would show 'Finished' immediately
CIt would still show 'Paused at yield 3'
DIt would reset to 'Start'
💡 Hint
Variable tracker shows where the generator pauses after each yield (see current Step 6).
Concept Snapshot
PHP Generator Function:
- Use 'yield' to pause and send values one by one.
- Execution pauses at each yield until resumed.
- Caller uses foreach or next() to get values.
- Generator ends when no more yields.
- Saves memory by producing values on demand.
Full Transcript
This visual execution shows how a PHP generator function works step-by-step. Calling the generator function creates and returns a Generator object without starting execution. When the foreach starts its first iteration, execution begins and runs until it hits the first yield, then it pauses and sends that value to the caller. The caller receives the value and can use it, then asks the generator to continue. The generator resumes from where it paused, runs until the next yield, and pauses again. This repeats until the generator finishes all yields and ends. The foreach loop automatically handles this process, stopping when the generator is done. Variables track the generator's state, showing it pauses after each yield and finishes at the end.