Arrow Function¶

Definition¶
Arrow functions were introduced in PHP 7.4 as a more concise syntax for anonymous functions • php.net
Usage¶
Besides the obvious advantage of being shorter and more readable than classic anonymous functions, arrow functions can access variables from the parent scope through a technique called “implicit by-value scope binding” • codepunker.com
Examples¶
$posts = [/* … */];
// classic closure
$ids = array_map(function ($post) {
return $post->id;
}, $posts);
// short closure/arrow function
$ids = array_map(fn($post) => $post->id, $posts);
References¶
Related articles¶
- Anonymous function function callback anonymous closure
- Closure function anonymous callback