Generator function execution model in PHP - Time & Space Complexity
We want to understand how the time cost changes when using a generator function in PHP.
Specifically, how does the generator's execution grow as it yields values?
Analyze the time complexity of the following generator function.
function countToN(int $n) {
for ($i = 1; $i <= $n; $i++) {
yield $i;
}
}
$gen = countToN(5);
foreach ($gen as $value) {
echo $value . "\n";
}
This code yields numbers from 1 to n one by one, pausing after each yield.
Look at what repeats during execution.
- Primary operation: The loop runs from 1 to n, yielding each number.
- How many times: Exactly n times, once per number.
Each time we ask the generator for the next value, it runs one loop step.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loop steps, 10 yields |
| 100 | 100 loop steps, 100 yields |
| 1000 | 1000 loop steps, 1000 yields |
Pattern observation: The number of steps grows directly with n, one step per yield.
Time Complexity: O(n)
This means the time to get all values grows linearly with how many values we want.
[X] Wrong: "The generator runs all loop steps at once when created."
[OK] Correct: The generator pauses at each yield, so it only does one step per request, not all at once.
Understanding how generators pause and resume helps you explain efficient data processing in interviews.
"What if the generator yielded values from a nested loop? How would the time complexity change?"
Practice
yield keyword do inside a PHP generator function?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]
- Confusing yield with return
- Thinking yield creates variables
- Assuming yield calls other functions
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]
- Using return with yield incorrectly
- Trying arrow function syntax for generators
- Using non-existent keywords like 'generator'
function gen() {
yield 1;
yield 2;
yield 3;
}
foreach (gen() as $value) {
echo $value . ' ';
}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]
- Ignoring spaces in echo output
- Thinking foreach can't iterate generators
- Confusing output format with commas
function gen() {
yield 1;
return 2;
yield 3;
}
foreach (gen() as $v) {
echo $v;
}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]
- Thinking return causes syntax error
- Expecting all yields to run despite return
- Assuming foreach can't handle return in generator
function gen() {
for ($i = 0; $i < 3; $i++) {
yield $i => $i * 2;
}
}
foreach (gen() as $key => $value) {
echo "$key:$value ";
}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]
- Confusing value with key
- Thinking yield key=>value is invalid
- Ignoring spaces in output
Ready to prove it?
Pass the test and this pattern moves unknown
