A generator function lets you pause and resume code, producing values one at a time without using lots of memory.
Generator function execution model in PHP
Introduction
Syntax
PHP
<?php function generatorName() { yield $value; }
The yield keyword pauses the function and sends a value out.
Each time you call next() on the generator, it resumes from where it paused.
Examples
PHP
<?php function countToThree() { yield 1; yield 2; yield 3; }
PHP
<?php function letters() { yield 'a'; yield 'b'; yield 'c'; }
Sample Program
This program creates a generator that yields two strings. The foreach loop gets each value and prints it on a new line.
PHP
<?php function simpleGenerator() { yield 'Hello'; yield 'World'; } $generator = simpleGenerator(); foreach ($generator as $value) { echo $value . "\n"; }
Important Notes
Generators save memory because they produce one value at a time instead of all at once.
You can use foreach to loop over generator values easily.
Generators can be paused and resumed, unlike normal functions that run all at once.
Summary
Generators use yield to pause and send values one by one.
They help handle large or infinite sequences efficiently.
Use foreach to get values from a generator.
Practice
1. What does the
yield keyword do inside a PHP generator function?easy
Solution
Step 1: Understand the role of
Theyieldin generatorsyieldkeyword pauses the function execution and sends a value back to the caller without ending the function.Step 2: Compare with other options
Unlikereturn,yielddoes not end the function but allows it to resume later.Final Answer:
It pauses the function and returns a value to the caller. -> Option AQuick Check:
yieldpauses and returns value [OK]
Hint: Remember: yield pauses, return ends function [OK]
Common Mistakes:
- Confusing yield with return
- Thinking yield creates variables
- Assuming yield calls other functions
2. Which of the following is the correct syntax to define a generator function in PHP?
easy
Solution
Step 1: Identify correct generator syntax
In PHP, a generator function is a normal function that usesyieldinside its body.Step 2: Check each option
function gen() { yield 1; } uses a normal function withyieldcorrectly. function gen() { return yield 1; } misusesreturn yield. function gen() => yield 1; uses invalid arrow syntax. generator gen() { yield 1; } uses an invalid keywordgenerator.Final Answer:
function gen() { yield 1; } -> Option BQuick Check:
Normal function + yield = generator [OK]
Hint: Generator = function with yield inside braces [OK]
Common Mistakes:
- Using return with yield incorrectly
- Trying arrow function syntax for generators
- Using non-existent keywords like 'generator'
3. What will be the output of the following PHP code?
function gen() {
yield 1;
yield 2;
yield 3;
}
foreach (gen() as $value) {
echo $value . ' ';
}medium
Solution
Step 1: Understand generator yields
The generator yields values 1, then 2, then 3 in sequence.Step 2: Analyze foreach output
The foreach loops over each yielded value and echoes it followed by a space, so output is "1 2 3 "Final Answer:
1 2 3 -> Option DQuick Check:
foreach prints each yield with space [OK]
Hint: foreach prints each yield value in order [OK]
Common Mistakes:
- Ignoring spaces in echo output
- Thinking foreach can't iterate generators
- Confusing output format with commas
4. Identify the error in this PHP generator function:
function gen() {
yield 1;
return 2;
yield 3;
}
foreach (gen() as $v) {
echo $v;
}medium
Solution
Step 1: Understand return in generators
Return ends the generator function immediately, so code after return is not executed.Step 2: Analyze code flow
After yielding 1, the return 2 ends the generator, so yield 3 is never reached or yielded.Final Answer:
The return ends generator early, so yield 3 is never reached -> Option CQuick Check:
Return stops generator, later yields skipped [OK]
Hint: Return ends generator; yields after return ignored [OK]
Common Mistakes:
- Thinking return causes syntax error
- Expecting all yields to run despite return
- Assuming foreach can't handle return in generator
5. Given this generator function, what will be the output?
function gen() {
for ($i = 0; $i < 3; $i++) {
yield $i => $i * 2;
}
}
foreach (gen() as $key => $value) {
echo "$key:$value ";
}hard
Solution
Step 1: Understand yield with key => value
The generator yields keys 0,1,2 with values 0,2,4 respectively (value = key * 2).Step 2: Analyze foreach output
foreach prints each key and value separated by colon and space, so output is "0:0 1:2 2:4 "Final Answer:
0:0 1:2 2:4 -> Option AQuick Check:
Yield key=>value prints key:value pairs [OK]
Hint: Yield key=>value pairs print as key:value [OK]
Common Mistakes:
- Confusing value with key
- Thinking yield key=>value is invalid
- Ignoring spaces in output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
