Arrow function in PHP 7.4

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!!!

--

--

--

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.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akash Gadhiya

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.

More from Medium

Laravel 9 released!

Beginners Guide for PHP

PHP vs JavaScript: The Best Language For Your Project

A Mini Php Mvc Structure: Zerdesht