Arrow Functions

PHP 7.4's arrow functions, fn($x) => $x * 2, have a single expression as their body, returned automatically. They capture every outer variable they mention by value, with no use, which suits array_map(), array_filter() and usort() callbacks. They cannot hold statements or change an outer variable.

An arrow-function callback, capture by value and nestingPHP
<?php
declare(strict_types=1);
$prices = ['pen' => 1.2, 'book' => 12.5, 'bag' => 30.0];
$off = 0.2;
echo json_encode(array_map(fn(float $p): float => $p * (1 - $off), $prices)), "\n";
$n = 1;
$bump = fn(): int => ++$n;                                // bumps a captured copy
$adder = fn(int $x): Closure => fn(int $y): int => $x + $y;
echo $bump(), ' ', $bump(), ' ', $n, ' ', $adder(3)(4), "\n";
Output
{"pen":0.96,"book":10,"bag":24}
2 2 1 7

$bump() returns 2 twice because each call starts from the captured copy, and $n stays 1. For statements or a shared counter, use function () use (&$n).