📖 Scenario: Imagine you are managing a list of tasks for a small project. You want to process each task one by one but also get a final summary message when all tasks are done.
🎯 Goal: You will create a PHP generator that yields tasks one by one and then returns a final message. You will learn how to get this return value from the generator.
📋 What You'll Learn
Create a generator function that yields tasks
Use a variable to store the final return message
Iterate over the generator using a foreach loop
Capture and display the generator's return value
💡 Why This Matters
🌍 Real World
Generators help process large or unknown amounts of data step-by-step without loading everything into memory. The return value can summarize the process.
💼 Career
Understanding generators and their return values is useful for PHP developers working on efficient data processing, asynchronous tasks, or pipelines.
Progress0 / 4 steps
1
Create a generator function with tasks
Write a generator function called taskGenerator that yields these exact tasks as strings: 'Setup environment', 'Write code', 'Test code'.
PHP
Hint
Use the yield keyword inside the function to return each task one by one.
2
Add a return statement with a final message
Inside the taskGenerator function, after yielding all tasks, add a return statement that returns the string 'All tasks completed'.
PHP
Hint
Use return 'All tasks completed'; after the last yield.
3
Iterate over the generator and capture the return value
Create a variable called $generator and assign it the result of calling taskGenerator(). Then use a foreach loop with $task to iterate over $generator and print each task with echo. After the loop, get the return value from $generator using $generator->getReturn() and store it in a variable called $finalMessage.
PHP
Hint
Use foreach ($generator as $task) to loop and $generator->getReturn() to get the final message.
4
Print the final return message
Write a line to print the $finalMessage variable using echo.
PHP
Hint
Use echo $finalMessage . "\n"; to print the message on a new line.
Practice
(1/5)
1. What does the return statement inside a PHP generator function do?
easy
A. It resets the generator to the beginning.
B. It immediately stops the generator and throws an error.
C. It sets the final value accessible via getReturn() after iteration.
D. It yields a value to the caller like yield does.
Solution
Step 1: Understand generator return behavior
In PHP, return inside 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 the getReturn() method on the generator object.
Final Answer:
It sets the final value accessible via getReturn() after iteration. -> Option C
Quick Check:
Generator return sets final value = D [OK]
Hint: Remember: return in generators sets final value, not yields [OK]
Common Mistakes:
Thinking return yields a value
Assuming return throws an error
Confusing return with yield
2. Which of the following is the correct syntax to get the return value of a generator after iteration?
easy
A. $gen->getReturn();
B. $gen->return();
C. $gen->yieldReturn();
D. $gen->finalValue();
Solution
Step 1: Identify the method to get generator return
PHP provides getReturn() method on generator objects to retrieve the final return value after iteration.
Step 2: Check other options
Other methods like return(), yieldReturn(), or finalValue() do not exist in PHP generators.
Final Answer:
$gen->getReturn(); -> Option A
Quick Check:
Use getReturn() to get final value [OK]
Hint: Use getReturn() method on generator object [OK]
Common Mistakes:
Using non-existent methods like return()
Trying to get return value before iteration
Confusing yield with return
3. What will be the output of the following PHP code?
D. The generator must yield at least three values.
Solution
Step 1: Analyze code flow after return
Once return 2; executes, the function ends, so yield 3; is never reached.
Step 2: Confirm other statements
Using return in generators is valid, getReturn() exists, and no minimum yield count is required.
Final Answer:
The yield 3; after return is unreachable code. -> Option A
Quick Check:
Code after return is unreachable = B [OK]
Hint: Code after return in generator is never executed [OK]
Common Mistakes:
Thinking yield after return runs
Believing return is not allowed in generators
Ignoring unreachable code warnings
5. You want to create a generator that yields numbers 1 to 3 and then returns their sum. Which code correctly achieves this?
hard
A. function gen() {
yield 1;
yield 2;
yield 3;
return 7;
}
B. function gen() {
for ($i = 1; $i <= 3; $i++) {
yield $i;
}
return $i;
}
C. function gen() {
yield 1;
yield 2;
yield 3;
return 6;
yield 7;
}
D. function gen() {
$sum = 0;
for ($i = 1; $i <= 3; $i++) {
$sum += $i;
yield $i;
}
return $sum;
}
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 unreachable yield 7; after return. The fourth returns 7, wrong sum.
Final Answer:
The code that accumulates the sum during yielding -> Option D
Quick Check:
Yield loop + sum return = A [OK]
Hint: Sum while yielding, then return sum after loop ends [OK]
Common Mistakes:
Placing code after return (unreachable)
Returning wrong sum value
Not accumulating sum during yield
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.