Arrow functions, also known as short closures, were introduced in PHP 7.4 as a more concise syntax for defining anonymous functions. They provide a shorthand way to create simple, one-line functions without the need for the function keyword or explicit return statements.
Example of an arrow function in PHP
$addition = fn($a, $b) => $a + $b;
echo $addition(2, 3); // Output: 5
In this example, the arrow function fn($a, $b) => $a + $b
takes two arguments $a
and $b
and returns their sum directly. The result is then echoed using the echo statement.
Arrow functions have the following characteristics:
- They are always anonymous: Arrow functions do not have a name and cannot be referenced or called by name. They are intended for use as short, inline functions.
- They use the fn keyword: Arrow functions are declared using the fn keyword, followed by a list of parameters in parentheses ($a, $b in the example above), an arrow (=>), and the expression or statement to be executed.
- They inherit variables from the parent scope: Arrow functions automatically capture variables from the surrounding scope, similar to regular closures. This means they have access to variables defined outside the function's body.
- They have implicit return: If the arrow function consists of a single expression, the result of that expression is automatically returned. There's no need to use the return keyword explicitly.
However, arrow functions have some limitations:
- They cannot contain statements: Arrow functions are limited to a single expression and cannot contain multiple statements or complex control flow.
- They cannot use $this: Unlike regular closures, arrow functions do not have their own scope and cannot access the $this keyword.
Array mapping with array_map()
$numbers = [1, 2, 3, 4, 5];
$multiplied = array_map(fn($number) => $number * 2, $numbers);
print_r($multiplied);
In this example, the arrow function fn($number) => $number * 2
is used as a callback in array_map()
. It multiplies each element of the $numbers
array by 2 and returns a new array with the transformed values.
Filtering with array_filter()
$numbers = [1, 2, 3, 4, 5];
$evenNumbers = array_filter($numbers, fn($number) => $number % 2 === 0);
print_r($evenNumbers);
Here, the arrow function fn($number) => $number % 2 === 0
is used as a callback in array_filter()
. It filters out the odd numbers from the $numbers
array and returns a new array with only the even numbers.
Sorting with usort()
$fruits = ['apple', 'orange', 'banana', 'grape'];
usort($fruits, fn($a, $b) => strcmp($a, $b));
print_r($fruits);
In this example, the arrow function fn($a, $b) => strcmp($a, $b)
is used as a comparison callback in usort()
. It sorts the $fruits array alphabetically using the strcmp()
function to compare the elements.
Array reduction with array_reduce()
$numbers = [1, 2, 3, 4, 5];
$total = array_reduce($numbers, fn($carry, $number) => $carry + $number, 0);
echo $total;
Here, the arrow function fn($carry, $number) => $carry + $number
is used as a callback in array_reduce()
. It calculates the sum of all the numbers in the $numbers
array and returns the final result.
Arrow functions are particularly useful in situations where a concise, one-line function is needed, such as in array manipulation functions or callback declarations.
They improve code readability and reduce the verbosity of anonymous function definitions.
However, it's important to use them judiciously and be aware of their limitations to ensure they are suitable for the task at hand.