fn
The "fn" keyword is a feature that was introduced in PHP 7.4, and is used to create arrow functions. Arrow functions are a shorthand notation for creating
The PHP fn Keyword (Arrow Functions)
The fn keyword, introduced in PHP 7.4, creates arrow functions — a concise syntax for writing single-expression anonymous functions. An arrow function returns the value of one expression and automatically captures the variables it uses from the surrounding scope. That makes it ideal for short callbacks passed to array functions like array_map, array_filter, and array_reduce.
This page covers the syntax, how variable capture differs from classic closures, the main rules and gotchas, and practical examples.
Syntax
An arrow function uses fn, a parameter list, the => (fat arrow) operator, and a single expression whose value is returned implicitly:
$arrowFunction = fn($parameter) => expression;There is no { ... } body and no return statement — both are forbidden. The expression after => is the return value.
$square = fn($n) => $n * $n;
echo $square(4); // 16Automatic variable capture (no use needed)
The biggest difference from a classic closure (function () use (...) { ... }) is that an arrow function automatically captures by value every variable from the parent scope that it references. You never write a use clause:
Because capture is by value only, arrow functions cannot modify an outer variable by reference. If you need that, use a full closure with use (&$var) instead.
Examples
Arrow functions shine as one-line callbacks. The expression is returned automatically, so no return is needed.
<?php
// array_reduce — sum a list
$numbers = [1, 2, 3, 4, 5];
$sum = array_reduce($numbers, fn($carry, $number) => $carry + $number);
echo $sum . PHP_EOL; // 15
// array_filter — keep names longer than 4 characters
$names = ["John", "Maryjane", "Paul", "Jane"];
$filtered = array_filter($names, fn($name) => strlen($name) > 4);
print_r($filtered); // Array ( [1] => Maryjane )
// array_map — apply 20% tax to each price
$prices = [10, 20, 30];
$withTax = array_map(fn($p) => $p * 1.2, $prices);
print_r($withTax); // Array ( [0] => 12 [1] => 24 [2] => 36 )
// Nested arrow functions (currying) — each captures from its parent
$adder = fn($x) => fn($y) => $x + $y;
echo $adder(3)(4) . PHP_EOL; // 7For more on passing functions as callbacks, see PHP callback functions.
Rules and gotchas
- One expression only. Arrow functions cannot contain statements, loops, or multiple lines. If your logic needs a block, use a full closure with
function () use (...) { ... }. - Capture is by value. Modifying a captured variable inside the arrow function does not affect the outer one, and later changes to the outer variable are not seen.
- No reference capture. You can't use
use (&$var); reach for a full closure when you need to mutate outer state. - Type hints and defaults are allowed:
fn(int $n = 0): int => $n + 1. - PHP 7.4+ only. On older versions, fall back to
function () use (...) { ... }.
fn vs. function (closures)
Arrow function (fn) | Closure (function) | |
|---|---|---|
| Variable capture | Automatic, by value | Explicit via use |
| Body | Single expression | Full statement block |
return keyword | Implicit | Required |
| Capture by reference | No | Yes, with use (&$var) |
| Available since | PHP 7.4 | PHP 5.3 |
Reach for fn when you have a short, single-expression callback. Use a full closure when you need multiple statements, reference capture, or want to be explicit about what you capture.
See also: PHP functions · variable scope.