Arrow function in PHP 7.4

Akash Gadhiya
1 min readOct 19, 2019
PHP 7.4

Arrow functions, introduced in PHP 7.4, provides a concise way to write functions in PHP 7.4. Short closures, also called Arrow functions, are a way of writing shorter functions in PHP 7.4.

This notation is useful when passing closures to functions like array_map or array_filter.

Arrow functions can be used:

  • Available as of PHP 7.4
  • No return keyword allowed
  • Have one expression, which is the return statement
  • Start with the fn keyword

Here are some examples, so you can differentiate Arrow function:

1) array_map function

Current

$ids = array_map(function ($post) {
return $post->id;
}, $posts);

Using Arrow function

// A collection of post objects$posts = [/*...*/];
$ids = array_map(fn ($post) => $post->id, $posts);

Kindly checkout below blog to get more example about Arrow function:

https://codeshipguru.blogspot.com/2019/10/arrow-function-in-php-74.html

Thank you!!!

--

--

Akash Gadhiya

I enjoy using my obsessive attention to detail, my unequivocal love for making things, and my mission-driven work ethic to literally change the World.