Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Using arrow functions in PHP 7.4

When PHP 7.4 released, it came with a whole lot of features/improvements that makes the language more interesting to work with. The one such feature that I want to talk about is arrow functions. For a primer, arrow functions are not new. In fact, If you’ve been working with the latest JavaScript (EcmaScript 6), you might’ve worked with arrow functions already.

Before PHP 7.4

With PHP 7.4, you can now use a shorthand syntax for defining functions with implicit by-value scope binding. Let’s understand it by taking the following example.

<?php

$factor = 10;

$numbers = array_map(function($value) use ($factor){
    return $value * $factor;
}, [1, 2, 3]);

print_r($numbers);
//Array([0] => 10 [1] => 20 [2] => 30)

?>

As you can see, in the above example, if you want to use higher order functions such as array_map, there will be an anonymous function which will evaluate the values from the array passed as a second argument and if you want to use a variable which lies outside of the scope of the anonymous function, you’ll need to add use. That’s a lot of boilerplate code, right?

Hello, Arrow functions!

From PHP 7.4, the above can be reduced to the following using fat arrow(=>) syntax like so.

<?php

$factor = 10;

$numbers = array_map(fn($value) => $value * $factor, [1, 2, 3]);

print_r($numbers);
//Array([0] => 10 [1] => 20 [2] => 30)

?>

The entire code is now reduced to just one line. It’s more readable, shorter and cleaner now. And as an added bonus, you now don’t have to pass the local varible (in above example $factor) using use explicitly. It’ll be accessible in the arrow function automatically.

Also, because arrow functions have an implicit “return”, it makes them ideal for single expressions rather than blocks of procedural code.

The limitation

There’s one limitation however using arrow functions. i.e. Currently, PHP only supports arrow functions for anonymous functions which consists of only one line. So, something like following is not supported yet.

<?php

$factor = 10;

$numbers = array_map(fn($value) => { 
    $factor = $factor * 2;

    return $value * $factor;
}, [1, 2, 3]);

print_r($numbers);
//Array([0] => 10 [1] => 20 [2] => 30)

?>

Hopefully, this will get fixed in the next iterations of PHP.

Fingers crossed!

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?