Bird
Raised Fist0
PHPprogramming~10 mins

Yield from delegation in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to yield values from the generator.

PHP
<?php
function gen1() {
    yield 1;
    yield 2;
}

function gen2() {
    [1] gen1();
    yield 3;
}

foreach (gen2() as $value) {
    echo $value . " ";
}
?>
Drag options to blanks, or click blank then click option'
Ayield from
Byield
Creturn
Decho
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'yield' instead of 'yield from' will yield the generator object itself, not its values.
Using 'return' will stop the function and not yield values.
Using 'echo' is invalid syntax here.
2fill in blank
medium

Complete the code to delegate yielding from the inner generator.

PHP
<?php
function inner() {
    yield 'a';
    yield 'b';
}

function outer() {
    [1] inner();
    yield 'c';
}

foreach (outer() as $char) {
    echo $char;
}
?>
Drag options to blanks, or click blank then click option'
Aprint
Breturn
Cyield from
Dyield
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'yield' alone yields the generator object, not its values.
Using 'return' stops the function and does not yield values.
Using 'print' is not valid syntax here.
3fill in blank
hard

Fix the error in the code to correctly delegate yielding.

PHP
<?php
function numbers() {
    yield 1;
    yield 2;
}

function combined() {
    [1] numbers();
    yield 3;
}

foreach (combined() as $num) {
    echo $num . ' ';
}
?>
Drag options to blanks, or click blank then click option'
Areturn
Byield from
Cyield
Decho
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'yield' alone yields the generator object, not its values.
Using 'return' stops the function prematurely.
Using 'echo' is invalid here.
4fill in blank
hard

Fill both blanks to create a generator that yields squares of numbers greater than 3.

PHP
<?php
function squares() {
    return [1, 2, 3, 4, 5];
}

function filteredSquares() {
    foreach (squares() as $num) {
        if ($num [1] 3) {
            yield $num [2] 2;
        }
    }
}

foreach (filteredSquares() as $val) {
    echo $val . ' ';
}
?>
Drag options to blanks, or click blank then click option'
A>
B**
C<
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will yield squares of numbers less than 3.
Using '+' instead of '**' will add 2 instead of squaring.
Using incorrect operators causes wrong output.
5fill in blank
hard

Fill both blanks to create a generator that yields uppercase keys and values greater than 10.

PHP
<?php
function data() {
    return ['a' => 5, 'b' => 15, 'c' => 20];
}

function filteredData() {
    foreach (data() as $key => $value) {
        if ($value [1] 10) {
            yield [2] => $value;
        }
    }
}

foreach (filteredData() as $k => $v) {
    echo "$k:$v ";
}
?>
Drag options to blanks, or click blank then click option'
A>
Bstrtoupper($key)
C<
D$key
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' filters wrong values.
Yielding original key instead of uppercase key.
Mixing up operators causes wrong filtering.

Practice

(1/5)
1.

What does the yield from statement do in PHP generators?

easy
A. It delegates yielding values from another generator or iterable.
B. It stops the generator immediately.
C. It converts a generator into an array.
D. It throws an exception inside the generator.

Solution

  1. Step 1: Understand the purpose of yield from

    yield from is used to delegate yielding values from another generator or iterable, so you don't have to write a loop to yield each value manually.
  2. Step 2: Compare with other options

    Stopping a generator, converting it to an array, or throwing exceptions are unrelated to yield from.
  3. Final Answer:

    It delegates yielding values from another generator or iterable. -> Option A
  4. Quick Check:

    yield from delegates values [OK]
Hint: Remember: yield from passes values from another generator [OK]
Common Mistakes:
  • Thinking yield from stops the generator
  • Confusing yield from with array conversion
  • Assuming it throws exceptions
2.

Which of the following is the correct syntax to delegate yielding from another generator $gen inside a generator function?

function gen() {
    // your code here
}
easy
A. yield $gen;
B. yield to $gen;
C. yield($gen);
D. yield from $gen;

Solution

  1. Step 1: Recall correct yield from syntax

    The correct syntax to delegate yielding from another generator is yield from $gen;.
  2. Step 2: Check other options for syntax errors

    yield $gen; yields the generator object itself, not its values. yield($gen); is invalid syntax. yield to $gen; is not valid PHP syntax.
  3. Final Answer:

    yield from $gen; -> Option D
  4. Quick Check:

    Correct syntax uses yield from [OK]
Hint: Use exact phrase yield from to delegate generators [OK]
Common Mistakes:
  • Using just yield $gen; instead of yield from
  • Adding parentheses incorrectly
  • Writing invalid keywords like 'yield to'
3.

What will be the output of the following PHP code?

function gen1() {
    yield 1;
    yield 2;
}

function gen2() {
    yield from gen1();
    yield 3;
}

foreach (gen2() as $value) {
    echo $value . ' ';
}
medium
A. 1 3 2
B. 3 1 2
C. 1 2 3
D. 1 2

Solution

  1. Step 1: Understand yield from gen1() in gen2()

    This delegates yielding values 1 and 2 from gen1() inside gen2().
  2. Step 2: Trace the output of the foreach loop

    The loop yields 1, then 2 from gen1(), then yields 3 from gen2(). So output is "1 2 3 "
  3. Final Answer:

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

    Delegation yields 1,2 then 3 [OK]
Hint: Remember yield from outputs all delegated values first [OK]
Common Mistakes:
  • Assuming order is changed
  • Forgetting the last yield after delegation
  • Thinking yield from yields the generator object
4.

Identify the error in this PHP generator code:

function genA() {
    yield 1;
    yield 2;
}

function genB() {
    yield genA();
    yield 3;
}

foreach (genB() as $val) {
    echo $val . ' ';
}
medium
A. It yields the generator object instead of its values.
B. Missing semicolon after yield statements.
C. Cannot yield inside a function.
D. The foreach loop is invalid syntax.

Solution

  1. Step 1: Analyze yield genA(); in genB()

    This yields the generator object genA() itself, not the values it produces.
  2. Step 2: Understand correct delegation with yield from

    To yield values from genA(), use yield from genA(); instead of yield genA();.
  3. Final Answer:

    It yields the generator object instead of its values. -> Option A
  4. Quick Check:

    Use yield from to delegate values [OK]
Hint: Use yield from to yield values, not the generator object [OK]
Common Mistakes:
  • Using yield instead of yield from for delegation
  • Assuming yielding a generator yields its values
  • Ignoring the need to delegate properly
5.

Given two generators genX() yielding numbers 1 to 3 and genY() yielding numbers 4 to 6, how can you create a generator genZ() that yields all numbers from 1 to 6 using yield from?

function genX() {
    yield 1;
    yield 2;
    yield 3;
}

function genY() {
    yield 4;
    yield 5;
    yield 6;
}

function genZ() {
    // Fill in code here
}
hard
A. yield genX(); yield genY();
B. yield from genX(); yield from genY();
C. yield genX() + genY();
D. yield from genY(); yield from genX();

Solution

  1. Step 1: Use yield from to delegate both generators in order

    To yield all numbers from 1 to 6 in sequence, delegate genX() first, then genY() using yield from.
  2. Step 2: Check order and syntax

    yield from genX(); yield from genY(); yields 1,2,3 then 4,5,6. yield from genY(); yield from genX(); reverses order. Options B and C yield generator objects or invalid expressions.
  3. Final Answer:

    yield from genX(); yield from genY(); -> Option B
  4. Quick Check:

    Delegate generators in order with yield from [OK]
Hint: Chain generators with multiple yield from statements [OK]
Common Mistakes:
  • Yielding generators instead of delegating
  • Reversing the order of delegation
  • Trying to add generators directly
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