0
0
PHPprogramming~10 mins

Memory efficiency with generators in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Memory efficiency with generators
Start generator function
Yield one value
Pause function, keep state
Request next value?
NoEnd generator
Yes
Resume function from pause
Yield next value
Repeat until done
The generator function yields one value at a time, pauses, and resumes on demand, saving memory by not storing all values at once.
Execution Sample
PHP
<?php
function countToThree() {
  yield 1;
  yield 2;
  yield 3;
}
foreach (countToThree() as $num) {
  echo $num . " ";
}
This code yields numbers 1 to 3 one by one, printing each without storing all at once.
Execution Table
StepGenerator StateYielded ValueActionOutput
1Start functionNoneYield 11
2Paused after yield 11Resume and yield 22
3Paused after yield 22Resume and yield 33
4Paused after yield 33Resume and end function
5Function endedNoneStop iteration
💡 Generator ends after yielding 3, no more values to yield.
Variable Tracker
VariableStartAfter 1After 2After 3Final
Generator StateNot startedPaused after yield 1Paused after yield 2Paused after yield 3Ended
Yielded ValueNone123None
Key Moments - 3 Insights
Why doesn't the generator store all values at once?
Because it yields one value and pauses (see execution_table steps 1-3), it only keeps current state, saving memory.
What happens when the generator finishes yielding all values?
It ends the function and stops iteration (see execution_table step 5), so no more values are produced.
How does the foreach loop get each value from the generator?
Each loop iteration resumes the generator from pause, gets the next yielded value, then pauses again (see execution_table steps 2-4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the generator state after step 2?
APaused after yield 2
BFunction ended
CPaused after yield 1
DNot started
💡 Hint
Check the 'Generator State' column in execution_table row for step 2.
At which step does the generator stop yielding values?
AStep 3
BStep 5
CStep 4
DStep 2
💡 Hint
Look at the 'Action' column where it says 'Stop iteration' in execution_table.
If the generator yielded 5 values instead of 3, how would the variable 'Yielded Value' change in variable_tracker?
AIt would have values up to 5 before final None
BIt would stay the same with only 3 values
CIt would end earlier
DIt would never yield any value
💡 Hint
Check how 'Yielded Value' changes after each yield in variable_tracker.
Concept Snapshot
PHP generators yield values one at a time.
They pause after each yield, saving memory.
foreach resumes generator to get next value.
No full list stored in memory.
Useful for large data or streams.
Full Transcript
This example shows how PHP generators work to save memory. The function countToThree yields numbers 1, 2, and 3 one by one. Each yield pauses the function, keeping its state. The foreach loop resumes the generator to get the next value. This way, the program never stores all values at once, making it memory efficient. The execution table traces each step: starting the function, yielding values, pausing, resuming, and finally ending. The variable tracker shows how the generator state and yielded values change over time. Key moments clarify why generators save memory and how iteration stops. The quiz tests understanding of generator states and output. Overall, generators help handle data step-by-step without heavy memory use.