0
0
PHPprogramming~20 mins

Generator return values in PHP - Practice Problems & Coding Challenges

Choose your learning style9 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.