0
0
PHPprogramming~20 mins

Closures in array functions in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Closure Mastery in PHP Arrays
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of array_map with closure capturing variable
What is the output of the following PHP code?
PHP
<?php
$multiplier = 3;
$numbers = [1, 2, 3];
$result = array_map(function($n) use ($multiplier) {
    return $n * $multiplier;
}, $numbers);
print_r($result);
?>
AArray ( [0] => 3 [1] => 6 [2] => 9 )
BArray ( [0] => 1 [1] => 2 [2] => 3 )
CArray ( [0] => 0 [1] => 0 [2] => 0 )
DFatal error: Undefined variable: multiplier
Attempts:
2 left
💡 Hint
Closures can capture variables from the surrounding scope using the 'use' keyword.
Predict Output
intermediate
2:00remaining
Effect of modifying captured variable by reference in closure
What will be the output of this PHP code?
PHP
<?php
$factor = 2;
$numbers = [1, 2, 3];
$result = array_map(function($n) use (&$factor) {
    return $n * $factor;
}, $numbers);
$factor = 5;
print_r($result);
?>
AArray ( [0] => 2 [1] => 4 [2] => 6 )
BArray ( [0] => 5 [1] => 10 [2] => 15 )
CArray ( [0] => 10 [1] => 20 [2] => 30 )
DFatal error: Cannot use variable by reference in closure
Attempts:
2 left
💡 Hint
The closure captures the variable by reference, but array_map applies the function immediately.
🔧 Debug
advanced
2:00remaining
Identify the error in closure used with array_filter
What error does the following PHP code produce?
PHP
<?php
$threshold = 10;
$values = [5, 15, 20];
$filtered = array_filter($values, function($v) {
    return $v > $threshold;
});
print_r($filtered);
?>
AArray ( [0] => 5 [1] => 15 [2] => 20 )
BParse error: syntax error, unexpected '{'
CArray ( [1] => 15 [2] => 20 )
DFatal error: Undefined variable: threshold
Attempts:
2 left
💡 Hint
Variables outside the closure must be imported with 'use' to be accessible inside.
📝 Syntax
advanced
2:00remaining
Syntax error in closure with array_reduce
Which option correctly fixes the syntax error in this PHP code?
PHP
<?php
$numbers = [1, 2, 3];
$sum = array_reduce($numbers, function($carry, $item) {
    $carry += $item
    return $carry;
});
echo $sum;
?>
ARemove the return statement inside the closure
BAdd a comma after '$carry += $item,' inside the closure
CAdd a semicolon after '$carry += $item;' inside the closure
DChange 'function' to 'fn' for arrow function syntax
Attempts:
2 left
💡 Hint
PHP statements must end with semicolons.
🚀 Application
expert
3:00remaining
Count how many elements satisfy a condition using closure
Given the array $data = [2, 7, 4, 9, 3], which option correctly counts how many numbers are greater than 5 using array_filter and a closure?
PHP
<?php
$data = [2, 7, 4, 9, 3];
$count = /* fill in here */;
echo $count;
?>
Aarray_reduce($data, fn($c, $x) => $c + ($x > 5), 0)
Bcount(array_filter($data, fn($x) => $x > 5))
Carray_filter($data, fn($x) => $x > 5)
Dcount(array_map(fn($x) => $x > 5, $data))
Attempts:
2 left
💡 Hint
array_filter returns filtered elements; count counts them.