0
0
PHPprogramming~20 mins

Memory efficiency with generators in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generator Guru
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of generator vs array memory usage
Consider the following PHP code that uses a generator and an array to produce numbers. What will be the output when running this code?
PHP
<?php
function genNumbers() {
    for ($i = 0; $i < 3; $i++) {
        yield $i;
    }
}

$generator = genNumbers();
$array = [0, 1, 2];

echo count($generator) . "," . count($array);
?>
A0,3
B3,3
C1,3
DFatal error: Uncaught Error: count(): Parameter must be an array or an object that implements Countable
Attempts:
2 left
💡 Hint
Remember that generators are not arrays and do not implement Countable interface.
🧠 Conceptual
intermediate
1:30remaining
Why use generators for large data?
Why are generators more memory efficient than arrays when processing large datasets in PHP?
AGenerators load all data into memory at once, making processing faster.
BGenerators automatically compress data to save memory.
CGenerators produce values one at a time, so they don't store the entire dataset in memory.
DGenerators store data on disk instead of memory.
Attempts:
2 left
💡 Hint
Think about how generators yield values compared to arrays.
🔧 Debug
advanced
2:30remaining
Fix the memory leak in generator usage
This PHP code uses a generator to process a large file line by line. However, memory usage grows unexpectedly. What is the cause?
PHP
<?php
function readLines($file) {
    $handle = fopen($file, 'r');
    while (($line = fgets($handle)) !== false) {
        yield $line;
    }
    fclose($handle);
}

foreach (readLines('largefile.txt') as $line) {
    // process line
}
?>
AThe generator does not close the file handle after iteration ends, causing a resource leak.
BThe file handle is closed too early inside the generator.
CUsing yield inside a while loop causes memory to accumulate.
DThe fgets function reads the entire file into memory.
Attempts:
2 left
💡 Hint
Check when fclose is called relative to the generator's lifecycle.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in generator function
Which option contains a syntax error in the generator function definition?
Afunction gen() { return yield 1; }
Bfunction gen() { yield from [1, 2, 3]; }
Cfunction gen() { yield 1; yield 2; }
Dfunction gen() { yield; }
Attempts:
2 left
💡 Hint
Check if 'return yield' is valid syntax in PHP generators.
🚀 Application
expert
3:00remaining
Calculate sum using generator for memory efficiency
You want to sum numbers from 1 to 1,000,000 without loading all numbers into memory at once. Which code correctly uses a generator to do this efficiently?
A
&lt;?php
$numbers = range(1, 1000000);
$sum = array_sum($numbers);
echo $sum;
?&gt;
B
&lt;?php
function numbers() {
    for ($i = 1; $i &lt;= 1000000; $i++) {
        yield $i;
    }
}
$sum = 0;
foreach (numbers() as $num) {
    $sum += $num;
}
echo $sum;
?&gt;
C
&lt;?php
function numbers() {
    return range(1, 1000000);
}
$sum = 0;
foreach (numbers() as $num) {
    $sum += $num;
}
echo $sum;
?&gt;
D
&lt;?php
function numbers() {
    for ($i = 1; $i &lt;= 1000000; $i++) {
        return $i;
    }
}
$sum = 0;
foreach (numbers() as $num) {
    $sum += $num;
}
echo $sum;
?&gt;
Attempts:
2 left
💡 Hint
Remember that 'yield' produces values one by one, while 'return' exits the function immediately.