Generator return values in PHP - Time & Space Complexity
Let's explore how the time needed to get values from a generator grows as we use it more.
We want to see how the generator's return value affects the work done.
Analyze the time complexity of the following code snippet.
function countToN(int $n): Generator {
for ($i = 1; $i <= $n; $i++) {
yield $i;
}
return 'Done';
}
$gen = countToN(5);
foreach ($gen as $value) {
echo $value . "\n";
}
$returnValue = $gen->getReturn();
This code yields numbers from 1 to n, then returns a final message after finishing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop yielding values one by one.
- How many times: Exactly n times, once for each number from 1 to n.
Each time we ask the generator for a value, it does one step of work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps to yield values + 1 step to return |
| 100 | 100 steps to yield values + 1 step to return |
| 1000 | 1000 steps to yield values + 1 step to return |
Pattern observation: The work grows directly with n, plus a tiny extra step at the end.
Time Complexity: O(n)
This means the time to get all values and the return grows in a straight line with how many values we want.
[X] Wrong: "The return value makes the generator run again for each call."
[OK] Correct: The generator only runs once through the loop; the return value is just stored and accessed after finishing.
Understanding how generators yield and return helps you explain efficient data processing in real projects.
"What if the generator yielded values conditionally inside nested loops? How would the time complexity change?"
Practice
return statement inside a PHP generator function do?Solution
Step 1: Understand generator
In PHP,returnbehaviorreturninside a generator does not yield a value but sets a final return value.Step 2: Accessing the return value
This final value can be accessed after iteration using thegetReturn()method on the generator object.Final Answer:
It sets the final value accessible viagetReturn()after iteration. -> Option CQuick Check:
Generatorreturnsets final value = D [OK]
return in generators sets final value, not yields [OK]- Thinking
returnyields a value - Assuming
returnthrows an error - Confusing
returnwithyield
Solution
Step 1: Identify the method to get generator return
PHP providesgetReturn()method on generator objects to retrieve the final return value after iteration.Step 2: Check other options
Other methods likereturn(),yieldReturn(), orfinalValue()do not exist in PHP generators.Final Answer:
$gen->getReturn(); -> Option AQuick Check:
UsegetReturn()to get final value [OK]
getReturn() method on generator object [OK]- Using non-existent methods like
return() - Trying to get return value before iteration
- Confusing
yieldwithreturn
$gen = (function() {
yield 1;
yield 2;
return 3;
})();
foreach ($gen as $value) {
echo $value . ' ';
}
echo $gen->getReturn();
Solution
Step 1: Iterate generator yields
The generator yields 1 and 2, so the loop prints "1 2 ".Step 2: Get the return value after iteration
After the loop,getReturn()returns 3, which is printed immediately after.Final Answer:
1 2 3 -> Option BQuick Check:
Yields + return value = 1 2 3 [OK]
getReturn() value [OK]- Expecting
returnto yield a value - Not calling
getReturn()after iteration - Assuming
getReturn()returns 0 by default
function gen() {
yield 1;
return 2;
yield 3;
}
$g = gen();
foreach ($g as $v) {
echo $v . ' ';
}
echo $g->getReturn();
Solution
Step 1: Analyze code flow after
Oncereturnreturn 2;executes, the function ends, soyield 3;is never reached.Step 2: Confirm other statements
Usingreturnin generators is valid,getReturn()exists, and no minimum yield count is required.Final Answer:
Theyield 3;afterreturnis unreachable code. -> Option AQuick Check:
Code afterreturnis unreachable = B [OK]
return in generator is never executed [OK]- Thinking
yieldafterreturnruns - Believing
returnis not allowed in generators - Ignoring unreachable code warnings
Solution
Step 1: Check yielding numbers 1 to 3
function gen() { $sum = 0; for ($i = 1; $i <= 3; $i++) { $sum += $i; yield $i; } return $sum; } uses a loop to yield 1, 2, 3 and sums them in$sum.Step 2: Verify other options
The second option yields 1-3 but returns$i(4 after loop), not sum 6. The third has unreachableyield 7;after return. The fourth returns 7, wrong sum.Final Answer:
The code that accumulates the sum during yielding -> Option DQuick Check:
Yield loop + sum return = A [OK]
- Placing code after
return(unreachable) - Returning wrong sum value
- Not accumulating sum during yield
Ready to prove it?
Pass the test and this pattern moves unknown
