Bird
Raised Fist0
PHPprogramming~20 mins

Closures as callbacks in PHP - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is a closure in PHP when used as a callback?
easy
A. A special variable type for storing data
B. A named function that cannot access outside variables
C. An anonymous function that can be passed as an argument
D. A PHP keyword for ending loops

Solution

  1. Step 1: Understand what a closure is

    A closure is an anonymous function, meaning it has no name and can be created on the fly.
  2. Step 2: Recognize closures as callbacks

    Closures can be passed as arguments to other functions to customize behavior, making them useful as callbacks.
  3. Final Answer:

    An anonymous function that can be passed as an argument -> Option C
  4. Quick Check:

    Closure = anonymous function callback [OK]
Hint: Closures are nameless functions passed as arguments [OK]
Common Mistakes:
  • Thinking closures must have names
  • Confusing closures with variables
  • Believing closures cannot be passed as arguments
2. Which of the following is the correct syntax to define a closure as a callback in PHP?
easy
A. array_map(function $x { return $x * 2; }, [1, 2, 3]);
B. array_map(function($x) { $x * 2; }, [1, 2, 3]);
C. array_map(function => $x * 2, [1, 2, 3]);
D. array_map(function($x) { return $x * 2; }, [1, 2, 3]);

Solution

  1. Step 1: Check closure syntax

    The correct syntax for an anonymous function uses the keyword function followed by parentheses with parameters, then braces with the function body.
  2. Step 2: Validate the return statement

    The closure must explicitly return the value to be used by array_map. array_map(function($x) { return $x * 2; }, [1, 2, 3]); correctly returns $x * 2.
  3. Final Answer:

    array_map(function($x) { return $x * 2; }, [1, 2, 3]); -> Option D
  4. Quick Check:

    Correct closure syntax = array_map(function($x) { return $x * 2; }, [1, 2, 3]); [OK]
Hint: Use function($param) { return value; } for closures [OK]
Common Mistakes:
  • Omitting parentheses around parameters
  • Missing return keyword inside closure
  • Using arrow (=>) incorrectly in closure
3. What will be the output of the following PHP code?
$numbers = [1, 2, 3];
$result = array_map(function($n) { return $n + 5; }, $numbers);
print_r($result);
medium
A. Array (1, 2, 3, 5)
B. [6, 7, 8]
C. [5, 6, 7]
D. [1, 2, 3]

Solution

  1. Step 1: Understand array_map with closure

    The closure adds 5 to each element of the array $numbers.
  2. Step 2: Calculate each element

    1+5=6, 2+5=7, 3+5=8, so the resulting array is [6, 7, 8].
  3. Final Answer:

    [6, 7, 8] -> Option B
  4. Quick Check:

    Each element + 5 = [6,7,8] [OK]
Hint: array_map applies closure to each element [OK]
Common Mistakes:
  • Confusing original array with result
  • Forgetting closure return value
  • Misreading array_map behavior
4. Identify the error in this PHP code using a closure as a callback:
$multiplier = 3;
$numbers = [1, 2, 3];
$result = array_map(function($n) { return $n * $multiplier; }, $numbers);
print_r($result);
medium
A. The variable $multiplier is not accessible inside the closure
B. array_map cannot accept closures as callbacks
C. Missing semicolon after array_map call
D. The closure must be named to access external variables

Solution

  1. Step 1: Check variable scope inside closure

    Variables outside a closure are not accessible inside unless passed with use.
  2. Step 2: Identify missing use keyword

    The closure needs use ($multiplier) to access the external variable.
  3. Final Answer:

    The variable $multiplier is not accessible inside the closure -> Option A
  4. Quick Check:

    External vars need use() in closures [OK]
Hint: Use 'use' to access outside variables in closures [OK]
Common Mistakes:
  • Assuming closures access all outside variables automatically
  • Thinking closures must be named to access variables
  • Believing array_map rejects closures
5. How can you modify this PHP code to correctly multiply each number by an external variable using a closure as a callback?
$factor = 4;
$nums = [2, 4, 6];
$results = array_map(function($num) { return $num * $factor; }, $nums);
print_r($results);
hard
A. Add use ($factor) after the function parentheses
B. Declare $factor as global inside the closure
C. Pass $factor as a second argument to array_map
D. Replace closure with a named function

Solution

  1. Step 1: Understand variable scope in closures

    To access an external variable inside a closure, you must explicitly import it using use.
  2. Step 2: Apply use ($factor) syntax

    Modify the closure to function($num) use ($factor) { return $num * $factor; } to access $factor.
  3. Final Answer:

    Add use ($factor) after the function parentheses -> Option A
  4. Quick Check:

    use() imports external vars into closures [OK]
Hint: Use 'use' keyword to bring external variables inside closures [OK]
Common Mistakes:
  • Trying to access external variables without use()
  • Using global keyword inside closures incorrectly
  • Passing extra arguments to array_map
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard