PHP Arrow Functions
In this page:
$closure = fn($parameter) => expression; // PHP 7.4+, captures outer variables automatically
$result = array_map(fn($item) => expression, $array);
What are Arrow Functions? (PHP 7.4+)
Introduced in PHP 7.4, arrow functions use the fn keyword and are limited to a single expression, whose result is returned automatically — there's no explicit return keyword, and no curly-brace body, which is what makes them so much shorter than a standard anonymous function for simple cases.
Example: What are Arrow Functions? (PHP 7.4+)
<?php
// Declare `$double`, set to `fn($x) => $x * 2`
$double = fn($x) => $x * 2;
// Print `$double(5)` to the output
echo $double(5);
?>
Login to try C/C++/Java/PHP code in the editor
Implicit Value Binding
Unlike a regular anonymous function, an arrow function automatically captures any variable from its surrounding scope that it actually references — by value — with no use clause needed.
This implicit capture is the main practical difference from a standard closure.
Example: Implicit Value Binding
<?php
// Declare `$tax`, set to `0.1`
$tax = 0.1;
// Declare `$addTax`, set to `fn($price) => $price + ($price * $tax)`
$addTax = fn($price) => $price + ($price * $tax);
// Print `$addTax(100)` to the output
echo $addTax(100);
?>
Login to try C/C++/Java/PHP code in the editor
Arrow Functions as Callbacks
Because arrow functions are so compact, they read naturally as an inline argument to something like array_map() or usort() — the entire callback fits on the same line as the function call that needs it, without a separate multi-line closure definition above.
Example: Arrow Functions as Callbacks
<?php
// Declare `$numbers` as an array: `[1, 2, 3, 4]`
$numbers = [1, 2, 3, 4];
// Declare `$doubled`, set to `array_map(fn($n) => $n * 2, $numbers)`
$doubled = array_map(fn($n) => $n * 2, $numbers);
// Print a human-readable dump of `$doubled`
print_r($doubled);
?>
Login to try C/C++/Java/PHP code in the editor
Typing Arrow Functions
You can add parameter and return type declarations to an arrow function exactly as you would to any other function — fn(int $x): int => $x * 2 — so the brevity of the syntax doesn't come at the cost of giving up type safety.
Example: Typing Arrow Functions
<?php
// Declare `$double`, set to `fn(int $x): int => $x * 2`
$double = fn(int $x): int => $x * 2;
// Print `$double(5)` to the output
echo $double(5);
?>
Login to try C/C++/Java/PHP code in the editor
Arrow Functions vs Anonymous Functions
The tradeoff for an arrow function's compactness is that it can only contain a single expression — no loops, no multiple statements, no intermediate variables.
Anything beyond that single expression requires falling back to a standard anonymous function instead.
Example: Arrow Functions vs Anonymous Functions
<?php
$sum = fn($a, $b) => $a + $b;
echo $sum(2, 3);
// A multi-statement version would need a regular function instead
?>
Login to try C/C++/Java/PHP code in the editor
- Trying to write multiple statements in an arrow function body, when
fnallows only one expression. - Expecting an arrow function to change an outer variable, as in
fn() => $count++, when it captures by value so the outer variable stays the same. - Using
fnon PHP versions before 7.4, which fails with a parse error.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: