Arrow functions, introduced in PHP 7.4, provide a shorter syntax for anonymous functions. These functions allow for cleaner, more concise code when you need simple callback functions.

Understand How Arrow Functions Work in PHP

Arrow functions make code easier to read and write. They reduce boilerplate, especially for small, one-line functions. They automatically capture variables from the surrounding scope, which eliminates the need to use the use() keyword in most cases.

This makes the code simpler and less error-prone, especially in callbacks or array operations.

Here is the syntax:

$function = fn($param) => $param * 2;

Short Closures in PHP

A short closure in PHP is a closure that uses the arrow function syntax. It is essentially a shorthand for anonymous functions, meant for simple, concise functions that return a value based on input.

Here's an example of a short closure using an arrow function:

$numbers = [1, 2, 3, 4, 5];

$square = fn($n) => $n * $n;

$squaredNumbers = array_map($square, $numbers);

print_r($squaredNumbers);

Arrow functions automatically capture variables from the surrounding scope by reference. This means you don't need to use the use() keyword to access variables from outside the function. This behavior makes short closures more intuitive and reduces boilerplate code. However, they can only capture variables from the surrounding scope and cannot modify them.

Arrow Functions vs Anonymous Functions

Arrow functions are shorter and more concise than traditional anonymous functions. Here’s how they compare:

Anonymous function:

$double = function($n) {
    return $n * 2;
};

Arrow function:

$double = fn($n) => $n * 2;

  1. Syntax: Arrow functions use fn and the => operator, while anonymous functions use function and curly braces {}.
  2. Return: In arrow functions, the return value is implied by the expression after =>, so you don't need the return keyword.
  3. Shorter: Arrow functions are more compact, especially for simple expressions.

Examples of PHP Arrow Functions

In the following example, we will use the filter array:

$numbers = [1, 2, 3, 4, 5];
$evenNumbers = array_filter($numbers, fn($n) => $n % 2 == 0);
print_r($evenNumbers); // Outputs: [2, 4]

This example uses the arrow function to filter out even numbers from the $numbers array.

The function checks if a number is divisible by 2. The arrow function makes the filtering logic concise, as it directly returns the result of the expression without needing return or curly braces.

In the other one, we will capture variables from the surrounding scope:

$multiplier = 2;
$double = fn($n) => $n * $multiplier;
echo $double(5); // Outputs: 10

In this case, the arrow function captures the $multiplier variable from the surrounding scope automatically. It multiplies the input $n by $multiplier. Arrow functions automatically capture variables by reference from the outer scope, making the code simpler than using the use() keyword in traditional closures.

Wrapping Up

Thank you for reading! Stay tuned for my upcoming articles and follow our blog, FlatCoding, for more PHP tutorials, or visit the PHP manual.