0
0
PHPprogramming~10 mins

Why generators are needed in PHP - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why generators are needed
Start function
Yield value
Pause execution
Resume on next call
Yield next value or end
Function ends
A generator function yields values one at a time, pausing and resuming execution to save memory and improve efficiency.
Execution Sample
PHP
<?php
function countToThree() {
  yield 1;
  yield 2;
  yield 3;
}
$gen = countToThree();
foreach ($gen as $value) {
  echo $value . "\n";
}
This code yields numbers 1 to 3 one by one, showing how generators produce values lazily.
Execution Table
StepActionGenerator StateValue YieldedOutput
1Call countToThree(), start executionStartednone
2Yield 1Paused after yielding 111
3Resume, yield 2Paused after yielding 222
4Resume, yield 3Paused after yielding 333
5Resume, function endsClosednone
💡 Function ends after yielding all values, generator closes.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
$genGenerator object createdYielded 1Yielded 2Yielded 3Closed
Key Moments - 2 Insights
Why does the function pause after yield instead of running all at once?
Because yield pauses the function, returning a value without losing its state, allowing resuming later (see steps 2-4 in execution_table).
What happens if we try to get a value after the generator is closed?
No more values are produced; the generator is finished (step 5 in execution_table shows function ends).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the generator state after step 3?
AClosed
BStarted
CPaused after yielding 2
DPaused after yielding 3
💡 Hint
Check the 'Generator State' column at step 3 in execution_table.
At which step does the generator function end?
AStep 2
BStep 5
CStep 4
DStep 3
💡 Hint
Look for 'function ends' in the 'Action' column in execution_table.
If the yield statements were replaced by return, what would happen?
AOnly the first value would be returned and function ends.
BThe function would pause and resume like a generator.
CAll values would be returned one by one lazily.
DThe function would yield all values at once.
💡 Hint
Recall that return exits the function immediately, unlike yield (see concept_flow).
Concept Snapshot
Generators produce values one at a time using yield.
Each yield pauses the function, saving memory.
They allow lazy iteration over large data.
Unlike return, yield keeps function state.
Useful for efficient loops and streams.
Full Transcript
This visual trace shows how PHP generators work by yielding values one at a time. The function starts, yields a value, then pauses. When resumed, it continues from where it left off, yielding the next value. This process repeats until all values are yielded and the function ends. This saves memory by not creating all values at once. The variable tracker shows the generator's state changing from started to paused after each yield, then closed at the end. Key moments clarify why the function pauses and what happens after it ends. The quiz tests understanding of generator states and behavior compared to return.