0
0
PHPprogramming~10 mins

Yield keyword behavior in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Yield keyword behavior
Function called
Start execution
Yield value
Pause function
Caller receives value
Caller requests next
Resume function
Repeat yield or end
Function ends
Iteration stops
The function runs until it hits a yield, returns a value, pauses, then resumes when the caller asks for the next value, repeating until done.
Execution Sample
PHP
<?php
function countToThree() {
  yield 1;
  yield 2;
  yield 3;
}

foreach (countToThree() as $number) {
  echo $number . " ";
}
This code yields numbers 1, 2, and 3 one by one, printing them with spaces.
Execution Table
StepActionYielded ValueFunction StateOutput
1Function called, starts executionN/AAt first yield
2Yield 11Paused after yield 11
3Resume functionN/AAt second yield1
4Yield 22Paused after yield 21 2
5Resume functionN/AAt third yield1 2
6Yield 33Paused after yield 31 2 3
7Resume functionN/AFunction ends1 2 3
8Iteration endsN/ADone1 2 3
💡 Function ends after yielding 3, iteration stops because no more yields.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
$numberundefined123undefined (iteration ended)
Key Moments - 3 Insights
Why does the function pause after yield instead of finishing?
Because yield returns a value and pauses the function, waiting for the caller to request the next value (see steps 2, 4, 6 in execution_table).
What happens when the caller asks for the next value?
The function resumes right after the last yield and continues until it hits the next yield or ends (see steps 3, 5, 7).
Why does the iteration stop after the last yield?
Because the function finishes execution with no more yields, so the iterator signals the end (step 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $number after step 4?
A1
B2
C3
Dundefined
💡 Hint
Check the variable_tracker row for $number at After Step 4.
At which step does the function first pause and yield a value?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the execution_table Action and Function State columns.
If the function had an extra yield after step 6, how would the iteration end?
AIt would continue with more steps until the new yield
BIt would stop immediately after step 6
CIt would end at step 8 as before
DIt would cause an error
💡 Hint
Yield pauses and resumes function; more yields mean more steps (see concept_flow).
Concept Snapshot
PHP yield keyword pauses function execution and returns a value.
Each yield produces one value for the caller.
Function resumes after yield when caller requests next.
Iteration ends when function finishes with no more yields.
Useful for creating generators that produce values on demand.
Full Transcript
This visual trace shows how the PHP yield keyword works inside a function. When the function is called, it runs until it hits the first yield, which returns a value and pauses the function. The caller receives this value and can use it. When the caller asks for the next value, the function resumes right after the last yield and continues until it hits the next yield or ends. This repeats until the function finishes execution with no more yields, ending the iteration. Variables like $number take on the yielded values step by step. This behavior allows efficient generation of values one at a time without running the whole function at once.