Arrow Functions in PHP. How to use them?

In PHP, we can create functions using arrow functions (also known as short closures).

Using arrow functions, we can define them in a single line, resulting in cleaner and easier-to-maintain code.

Arrow Function Syntax

The basic structure of an arrow function is as follows:

Code presenting the syntax of arrow functions.

Difference compared to the traditional syntax

Previously, we’d write this:

Code example of an anonymous function.

Now we can use an arrow function:

Code example of an arrow function.

Let’s elaborate on the syntax.

The fn keyword

Arrow functions use the fn keyword instead of the function keyword.

No need for return

In arrow functions, values are returned implicitly. There is no need for the return keyword.

No {} brackets are needed

Arrow functions can only have a single expression.

They cannot have multiple statements, blocks of code, loops, or conditionals that span multiple lines.

Thus, they have no need for brackets.

Example of an arrow function using brackets, thus initiating a parse error.

This one expression can be spread over multiple lines for better formatting, but it should be one expression.

Code example of one expression spreading in multiple lines.

No need for the use keyword

Arrow functions automatically inherit data from their outer scope; the use keyword is unnecessary.

Code exaple of an arrow function automatically inheritting data from its outer scope.

In a traditional anonymous function we explicitly use the use keyword.

Code example of an anonymous function using the use keyword.

Arrow functions automatically inherit the variable $multiplier residing out of the $multiplyArrow function, while traditional functions require the use keyword.

Arrow functions support the same features as anonymous functions

Multiple Parameters

Arrow functions can take multiple parameters.

Code example of an arrow function taking multiple parameters.

Nested Functions

Arrow functions support nesting.

Code example of nested arrow function.

A normal function equivalent is:

Code example showing the equivalent of the previous function with a normal function.

Conditional logic

We can use conditional logic, as long as it is a single expression. Thus we cannot use if…else statements but it is perfectly alright to use ternary operators or null coalescing.

Example of ternary operator in arrow functions:

Code example of a ternary operator with an arrow function.

Example of null coalescing in arrow functions:

Code example of ternary operator with arrow functon.

When to use them?

Arrow functions are particularly useful when working with array functions like array_map(), array_reduce(), etc.

Their single expression nature makes it easier to pass closures to these functions.

Code example or an arrow function used with the array_map function.

However, for complex logic, traditional closures with function remain the better choice.

Although my blog doesn’t support comments, feel free to reply via email or X.