📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners Arrow Functions

Arrow Functions

5 min read Quiz at the end
Arrow functions use fn() => syntax and are short anonymous functions for callbacks. They automatically capture variables from the outer scope without needing use(). Perfect for simple callbacks in array_map() and filter.

Arrow Functions (PHP 7.4+)

// Regular closure — must use "use" to capture variables
$multiplier = 3;
$times = function($n) use ($multiplier) {
    return $n * $multiplier;
};

// Arrow function — automatically captures outer scope
$multiplier = 3;
$times = fn($n) => $n * $multiplier;  // cleaner!

echo $times(5);  // 15

// Arrow functions in array operations
$prices = [10.5, 24.99, 5.0, 149.0];

$expensive = array_filter($prices, fn($p) => $p > 20);
$doubled   = array_map(fn($p) => $p * 2, $prices);

$tax = 0.08;
$withTax = array_map(fn($p) => $p * (1 + $tax), $prices);
// $tax captured automatically