0
0
PHPprogramming~20 mins

Why closures matter in PHP - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Closure Master in PHP
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a closure capturing a variable
What is the output of this PHP code that uses a closure capturing a variable by reference?
PHP
<?php
$counter = 0;
$increment = function() use (&$counter) {
    $counter++;
    return $counter;
};

echo $increment();
echo $increment();
?>
A11
B01
C00
D12
Attempts:
2 left
💡 Hint
Closures can capture variables by reference, so changes inside affect the original variable.
🧠 Conceptual
intermediate
1:30remaining
Why use closures in PHP?
Which of the following best explains why closures matter in PHP?
AThey prevent any variable from being modified inside the function.
BThey allow functions to access variables from the parent scope even after that scope has ended.
CThey automatically convert all variables to global scope.
DThey replace the need for classes and objects entirely.
Attempts:
2 left
💡 Hint
Think about how closures keep variables alive beyond their normal lifetime.
🔧 Debug
advanced
2:30remaining
Identify the error with closure variable capture
What error will this PHP code produce and why?
PHP
<?php
$funcs = [];
for ($i = 0; $i < 3; $i++) {
    $funcs[] = function() {
        return $i;
    };
}
foreach ($funcs as $f) {
    echo $f();
}
?>
AOutputs '333' because $i is not captured correctly
BSyntax error due to missing use keyword
COutputs '012' as expected
DNotice: Undefined variable $i
Attempts:
2 left
💡 Hint
Variables inside closures must be explicitly captured with use().
📝 Syntax
advanced
1:30remaining
Correct syntax for closure with parameters
Which option shows the correct syntax for a PHP closure that takes one parameter and returns its square?
A$square = function($x) { return $x * $x; };
B$square = function $x { return $x * $x; };
C$square = function($x) => $x * $x;
D$square = function { return $x * $x; }($x);
Attempts:
2 left
💡 Hint
Closures use the function keyword followed by parentheses for parameters.
🚀 Application
expert
3:00remaining
Using closures to create a counter generator
Which option correctly creates a closure in PHP that returns a function acting as a counter starting from 0 and increasing by 1 each call?
A
&lt;?php
function counter() {
    $count = 0;
    return function() {
        return ++$count;
    };
}
$c = counter();
echo $c();
echo $c();
?&gt;
B
&lt;?php
function counter() {
    static $count = 0;
    return function() {
        return ++$count;
    };
}
$c = counter();
echo $c();
echo $c();
?&gt;
C
&lt;?php
function counter() {
    $count = 0;
    return function() use (&amp;$count) {
        return ++$count;
    };
}
$c = counter();
echo $c();
echo $c();
?&gt;
D
&lt;?php
function counter() {
    $count = 0;
    return function() use ($count) {
        return ++$count;
    };
}
$c = counter();
echo $c();
echo $c();
?&gt;
Attempts:
2 left
💡 Hint
To keep the count variable updated across calls, capture it by reference.