Junior — Middle
Have you used anonymous functions in your projects?
sobes.tech AI
Answer from AI
Yes, in PHP, anonymous functions (closures) are widely used for creating functions on the fly, passing them as arguments, or defining callbacks.
Example of an anonymous function in PHP:
$numbers = [1, 2, 3, 4, 5];
$squared = array_map(function($n) {
return $n * $n;
}, $numbers);
print_r($squared);
Here, the anonymous function is passed to array_map to square each number. This approach is convenient when the function does not need to be reused elsewhere and is logically connected to a specific call.