An arrow function is a short anonymous function: fn($x) => $x * 2. The body after => is a single expression whose value is returned, and any variable from the surrounding code that the expression uses is captured automatically, by value. Arrow functions exist since PHP 7.4 and are mostly used as callbacks.
The syntax
fn, the parameter list, =>, one expression. Parameter types, default values and a return type work the same as in regular functions:
There is no return keyword and no braces. A long expression can still be split over several lines for readability.
Outer variables are captured by value
An arrow function sees every variable of the scope where it is created. It copies their values at that moment. Changes made inside stay inside, and changes made outside after the function was created are not seen:
This is the main difference from function () use (&$count), which can share a variable by reference. An arrow function cannot reassign an outer variable. It can still change an object it captured (the copy is a handle to the same object), and a parameter declared by reference works as in any function: fn(array &$list) => $list[] = 'x'.
Callbacks for array_map, array_filter and usort
Most array functions take a callback, and that is where arrow functions save the most noise. Compare one filter written both ways:
<=> returns -1, 0 or 1, exactly what usort() wants, so sorting by a field fits on one line. Swap $a and $b to sort descending.
Arrow functions that return arrow functions
The captured variables include the parameters of an enclosing arrow function, so you can nest them to build functions from other values:
Note that array_filter() keeps the original keys, which is why the result starts at index 1.
fn vs function: when you need a full closure
Arrow functions stop working when the body needs more than one expression or must change an outer variable. Then use an anonymous function:
fn($x) => ... | function ($x) use ($y) { ... } | |
|---|---|---|
| Body | one expression | any statements |
| Return | automatic | explicit return |
| Outer variables | all used ones, automatically | only those listed in use |
| Capture by reference | no | yes, use (&$y) |
| Available since | PHP 7.4 | PHP 5.3 |
When an arrow function feels cramped, it often means the job is two jobs. Splitting it into a filter and a sum, as above, keeps each callback a single expression.
Frequently Asked Questions
What is an arrow function in PHP?
A short anonymous function introduced in PHP 7.4: fn($x) => $x * 2. Its body is a single expression whose value is returned automatically, and it can read variables from the surrounding scope without a use clause.
Can a PHP arrow function have multiple lines?
No. The body after => must be one expression; there is no block with statements. You can split a long expression across lines, but if you need if, loops or several statements, use function () use (...) { ... } instead.
Can an arrow function modify an outer variable?
No. Arrow functions capture outer variables by value, so fn() => $count++ changes only its own copy and $count outside stays the same. To modify an outer variable, use a closure with use (&$count).
What is the difference between fn and function in PHP?
fn($x) => ... holds one expression, returns it implicitly and captures every outer variable it uses by value. function ($x) use ($y) { ... } can hold any statements, needs an explicit return, and only sees the variables listed in use, which may be captured by reference.