How can anonymous functions be used in PHP?
Anonymous functions in PHP can be used to create functions without giving them a specific name. They are useful for defining callbacks or functions that are only needed temporarily. Anonymous functions can be assigned to variables, passed as arguments to other functions, or used within other functions.
// Example of using an anonymous function as a callback
$numbers = [1, 2, 3, 4, 5];
$filteredNumbers = array_filter($numbers, function($num) {
return $num % 2 == 0;
});
print_r($filteredNumbers);