Field Manual PHP / Laravel reference
← PHP

Anonymous functions

Arrow functions express one expression and capture outer variables by value.

What it is

Arrow functions express one expression and capture outer variables by value.

When to use it

Use concise transformations or predicates.

Syntax

$names = array_map(fn ($user) => $user["name"], $users);

Example

$minimum = 1000;
$eligible = array_filter($orders, fn ($order) => $order["total_cents"] >= $minimum);

Common mistakes

Arrow functions return the expression automatically and cannot contain multiple statements.

Related topics

Browse PHP