0
0
PHPprogramming~20 mins

Closures as callbacks in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Closure Callback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a closure used as a callback
What is the output of this PHP code using a closure as a callback?
PHP
<?php
$numbers = [1, 2, 3, 4];
$result = array_map(function($n) {
    return $n * 2;
}, $numbers);
print_r($result);
?>
AArray ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 )
BArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
CArray ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 )
DFatal error: Uncaught Error: Call to undefined function array_map()
Attempts:
2 left
💡 Hint
array_map applies the callback to each element and returns a new array.
Predict Output
intermediate
2:00remaining
Closure capturing external variable
What will be the output of this PHP code where a closure captures an external variable?
PHP
<?php
$factor = 3;
$numbers = [1, 2, 3];
$result = array_map(function($n) use ($factor) {
    return $n * $factor;
}, $numbers);
print_r($result);
?>
AArray ( [0] => 0 [1] => 0 [2] => 0 )
BArray ( [0] => 3 [1] => 6 [2] => 9 )
CFatal error: Uncaught Error: Undefined variable: factor
DArray ( [0] => 1 [1] => 2 [2] => 3 )
Attempts:
2 left
💡 Hint
The closure uses the 'use' keyword to access $factor inside.
Predict Output
advanced
2:00remaining
Modifying external variable inside closure
What is the output of this PHP code where a closure modifies an external variable by reference?
PHP
<?php
$sum = 0;
$numbers = [1, 2, 3];
array_walk($numbers, function($n) use (&$sum) {
    $sum += $n;
});
echo $sum;
?>
A6
B0
CFatal error: Uncaught Error: Cannot use variable by reference
D3
Attempts:
2 left
💡 Hint
The closure uses & to modify $sum by reference.
Predict Output
advanced
2:00remaining
Closure as callback with default argument
What will this PHP code output when using a closure with a default argument as a callback?
PHP
<?php
$numbers = [1, 2, 3];
$result = array_map(function($n, $multiplier = 2) {
    return $n * $multiplier;
}, $numbers);
print_r($result);
?>
AWarning: array_map() expects at most 2 parameters, 3 given
BArray ( [0] => 2 [1] => 4 )
CArray ( [0] => 2 [1] => 4 [2] => 6 )
DArray ( [0] => 1 [1] => 2 [2] => 3 )
Attempts:
2 left
💡 Hint
array_map passes only one argument to the callback here.
🧠 Conceptual
expert
2:00remaining
Why use closures as callbacks in PHP?
Which is the best explanation for why closures are useful as callbacks in PHP?
AClosures automatically optimize code performance by compiling callbacks to machine code.
BClosures are required in PHP because named functions cannot be used as callbacks.
CClosures prevent any variable from being accessed inside the callback, ensuring strict isolation.
DClosures allow you to write inline functions that can capture variables from the surrounding scope, making callbacks more flexible and concise.
Attempts:
2 left
💡 Hint
Think about how closures can access variables outside their own function body.