0
0
PHPprogramming~10 mins

Yield from delegation in PHP - Interactive Code Practice

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