Complete the code to define a closure that prints a message.
<?php
$message = function() { echo [1]; };
$message();
?>The closure must echo a string, so the string must be quoted.
Complete the code to pass a closure as a callback to array_map.
<?php $numbers = [1, 2, 3]; $squares = array_map([1], $numbers); print_r($squares); ?>
The closure must be a function that returns the square of the number.
Fix the error in the closure that uses a variable from the parent scope.
<?php $multiplier = 3; $multiply = function($n) [1] { return $n * $multiplier; }; echo $multiply(5); ?>
In PHP, to use a variable from the parent scope inside a closure, you must use the use keyword.
Fill both blanks to create a closure that modifies a variable by reference.
<?php $count = 0; $increment = [1] use ([2]) { $count++; }; $increment(); echo $count; ?>
The closure is defined as a function with no parameters, but it uses $count by reference with &$count in the use clause.
Fill all three blanks to create a closure that filters an array using a condition.
<?php $words = ['apple', 'bee', 'cat', 'dog']; $minLength = 3; $longWords = array_filter($words, function([1]) use ([2]) { return strlen([3]) > [2]; }); print_r($longWords); ?>
The closure takes $word as parameter, uses $minLength from parent scope, and checks the length of $word.