Bird
Raised Fist0
PHPprogramming~20 mins

Generator return values in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Generator Return Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP generator code?

Consider the following PHP code using a generator with a return value. What will be the output?

PHP
<?php
function gen() {
    yield 1;
    yield 2;
    return 3;
}
$generator = gen();
foreach ($generator as $value) {
    echo $value . ' ';
}
echo $generator->getReturn();
A1 2
B3
C1 2 3
D1 2 0
Attempts:
2 left
💡 Hint

Remember that yield produces values one by one, and return sets the generator's return value accessible by getReturn().

❓ Predict Output
intermediate
2:00remaining
What does this PHP generator return after iteration?

What is the value returned by getReturn() after iterating this generator?

PHP
<?php
function numbers() {
    yield 10;
    yield 20;
    return 42;
}
$gen = numbers();
foreach ($gen as $num) {
    // just iterate
}
echo $gen->getReturn();
A42
Bnull
C20
D10
Attempts:
2 left
💡 Hint

The return statement in a generator sets the return value accessible by getReturn().

🔧 Debug
advanced
2:00remaining
Why does this PHP code cause an error when accessing generator return value?

Examine the code below. Why does calling getReturn() cause a fatal error?

PHP
<?php
function gen() {
    yield 1;
    return 5;
}
$g = gen();
echo $g->getReturn();
ABecause the generator was not fully iterated before calling getReturn()
BBecause getReturn() is not a valid method for generators
CBecause return values are not supported in PHP generators
DBecause yield must be the last statement in the generator
Attempts:
2 left
💡 Hint

Think about when the generator finishes execution and when the return value is available.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a generator with a return value in PHP?

Choose the correct PHP code snippet that defines a generator function which yields values and returns a value.

Afunction gen() { yield 1; return; yield 2; }
Bfunction gen() { yield 1; yield 2; yield return 3; }
Cfunction gen() { return 3; yield 1; yield 2; }
Dfunction gen() { yield 1; yield 2; return 3; }
Attempts:
2 left
💡 Hint

Remember the syntax for yield and return in generators.

🚀 Application
expert
3:00remaining
How to capture a generator's return value after partial iteration in PHP?

You have a generator that yields several values and then returns a final result. You want to iterate only the first two values and then get the return value. Which code snippet correctly achieves this?

PHP
<?php
function gen() {
    yield 'a';
    yield 'b';
    yield 'c';
    return 'done';
}
$g = gen();
// Your code here
Aecho $g->getReturn(); foreach ($g as $v) {}
B$g->next(); $g->next(); foreach ($g as $v) {} echo $g->getReturn();
Cforeach ($g as $v) { if ($v == 'b') break; } echo $g->getReturn();
Dforeach ($g as $i => $v) { if ($i == 1) break; } echo $g->getReturn();
Attempts:
2 left
💡 Hint

You must fully exhaust the generator to get the return value. Breaking early leaves it unfinished.

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

  1. Step 1: Understand generator return behavior

    In PHP, return inside a generator does not yield a value but sets a final return value.
  2. Step 2: Accessing the return value

    This final value can be accessed after iteration using the getReturn() method on the generator object.
  3. Final Answer:

    It sets the final value accessible via getReturn() after iteration. -> Option C
  4. 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

  1. Step 1: Identify the method to get generator return

    PHP provides getReturn() method on generator objects to retrieve the final return value after iteration.
  2. Step 2: Check other options

    Other methods like return(), yieldReturn(), or finalValue() do not exist in PHP generators.
  3. Final Answer:

    $gen->getReturn(); -> Option A
  4. 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?
$gen = (function() {
    yield 1;
    yield 2;
    return 3;
})();
foreach ($gen as $value) {
    echo $value . ' ';
}
echo $gen->getReturn();
medium
A. 3
B. 1 2 3
C. 1 2
D. 1 2 0

Solution

  1. Step 1: Iterate generator yields

    The generator yields 1 and 2, so the loop prints "1 2 ".
  2. Step 2: Get the return value after iteration

    After the loop, getReturn() returns 3, which is printed immediately after.
  3. Final Answer:

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

    Yields + return value = 1 2 3 [OK]
Hint: Print yields in loop, then append getReturn() value [OK]
Common Mistakes:
  • Expecting return to yield a value
  • Not calling getReturn() after iteration
  • Assuming getReturn() returns 0 by default
4. Identify the error in this PHP generator code:
function gen() {
    yield 1;
    return 2;
    yield 3;
}
$g = gen();
foreach ($g as $v) {
    echo $v . ' ';
}
echo $g->getReturn();
medium
A. The yield 3; after return is unreachable code.
B. You cannot use return inside a generator.
C. The getReturn() method does not exist.
D. The generator must yield at least three values.

Solution

  1. Step 1: Analyze code flow after return

    Once return 2; executes, the function ends, so yield 3; is never reached.
  2. Step 2: Confirm other statements

    Using return in generators is valid, getReturn() exists, and no minimum yield count is required.
  3. Final Answer:

    The yield 3; after return is unreachable code. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    The code that accumulates the sum during yielding -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard