← Back to PHP Course | Chapter 5: Functions | Lesson 8 of 10

PHP Arrow Functions

An arrow function is a very short way to write a tiny one-line helper. It is like a text-message version of a function, and it automatically peeks at variables from around it.
Syntax
php
$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
<?php
// Declare `$double`, set to `fn($x) => $x * 2`
$double = fn($x) => $x * 2;
// Print `$double(5)` to the output
echo $double(5);
?>

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
<?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);
?>

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
<?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);
?>

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
<?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);
?>

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
<?php
$sum = fn($a, $b) => $a + $b;
echo $sum(2, 3);
// A multi-statement version would need a regular function instead
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to write multiple statements in an arrow function body, when fn allows only one expression.
  2. 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.
  3. Using fn on PHP versions before 7.4, which fails with a parse error.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.