Bird
Raised Fist0
PHPprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the yield keyword do in a PHP function?
easy
A. It returns a value and pauses the function, resuming later to produce more values.
B. It immediately stops the function and returns a single value.
C. It creates a new variable inside the function.
D. It converts the function into a class method automatically.

Solution

  1. Step 1: Understand yield behavior

    The yield keyword pauses the function and returns a value without ending it.
  2. Step 2: Compare with return

    Unlike return, yield allows the function to resume and produce more values later.
  3. Final Answer:

    It returns a value and pauses the function, resuming later to produce more values. -> Option A
  4. Quick Check:

    Yield pauses and resumes function = A [OK]
Hint: Yield pauses function to produce values one by one [OK]
Common Mistakes:
  • Confusing yield with return which ends function
  • Thinking yield creates variables
  • Assuming yield converts function to class method
2. Which of the following is the correct syntax to yield values from a function in PHP?
easy
A. yield($value);
B. return yield $value;
C. yield => $value;
D. yield $value;

Solution

  1. Step 1: Recall yield syntax

    The correct syntax to yield a value is simply yield $value; inside the function.
  2. Step 2: Check other options

    Options B, C, and D use invalid or incorrect syntax for yield in PHP.
  3. Final Answer:

    yield $value; -> Option D
  4. Quick Check:

    Simple yield syntax is 'yield $value;' [OK]
Hint: Yield uses simple 'yield $value;' syntax [OK]
Common Mistakes:
  • Adding return before yield
  • Using arrow (=>) with yield
  • Calling yield like a function
3. What will be the output of this PHP code?
function gen() {
  yield 1;
  yield 2;
  yield 3;
}
foreach (gen() as $val) {
  echo $val . ' ';
}
medium
A. 1 2 3
B. Array
C. 123
D. Error: yield not allowed here

Solution

  1. Step 1: Understand generator yields

    The function gen() yields values 1, 2, and 3 one by one.
  2. Step 2: foreach prints each yielded value

    The foreach loop prints each value followed by a space, so output is '1 2 3 '.
  3. Final Answer:

    1 2 3 -> Option A
  4. Quick Check:

    Yielded values printed with spaces = B [OK]
Hint: Yield outputs values one by one in foreach loop [OK]
Common Mistakes:
  • Expecting concatenated string without spaces
  • Thinking yield returns an array
  • Assuming syntax error with yield
4. Identify the error in this PHP code using yield:
function numbers() {
  yield 1
  yield 2;
}
foreach(numbers() as $n) {
  echo $n;
}
medium
A. foreach loop syntax is wrong
B. Missing semicolon after first yield statement
C. Cannot use yield inside a function
D. Yield must be followed by parentheses

Solution

  1. Step 1: Check yield statements

    The first yield statement is missing a semicolon at the end, causing a syntax error.
  2. Step 2: Validate other parts

    Using yield inside a function and foreach syntax are correct; yield does not require parentheses.
  3. Final Answer:

    Missing semicolon after first yield statement -> Option B
  4. Quick Check:

    Each yield needs semicolon [OK]
Hint: Check semicolons after yield statements [OK]
Common Mistakes:
  • Forgetting semicolon after yield
  • Thinking yield needs parentheses
  • Assuming foreach syntax error
5. Given this PHP function using yield:
function filterPositive(array $numbers) {
  foreach ($numbers as $num) {
    if ($num > 0) {
      yield $num;
    }
  }
}

$values = [-2, 0, 3, 5, -1];
foreach (filterPositive($values) as $val) {
  echo $val . ',';
}

What is the output?
hard
A. 0,3,5,
B. -2,0,3,5,-1,
C. 3,5,
D. Error: yield cannot be used with if

Solution

  1. Step 1: Analyze filtering condition

    The function yields only numbers greater than 0, so it skips -2, 0, and -1.
  2. Step 2: Identify yielded values

    Only 3 and 5 satisfy the condition and are yielded, printed with commas.
  3. Final Answer:

    3,5, -> Option C
  4. Quick Check:

    Yield filters positive numbers only = D [OK]
Hint: Yield can filter values with if condition [OK]
Common Mistakes:
  • Including zero or negative numbers
  • Thinking yield can't be inside if
  • Expecting all values printed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard