Skip to content

PHP 7.4 – Arrow functions

Last updated on January 30, 2021

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)
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments