Posted by Arjun on Saturday 4th January 2020

PHP 7.4 - Arrow functions

PHP 7.4

Arrow functions also called "short closures". We can always access the parent scope, there's no need for the use keyword. Here is a simple example with an arrow function.

$items = [1,2,3,4,5];
$factor = 10;
array_map(fn ($item) => $item * $factor, $items)

Above code we can write as show below without arrow function

$items = [1,2,3,4,5];
array_map(function ($item) use ($factor) { 
    return $item* $factor; 
}, $items)