BradCypert.com
Arrow Functions in PHP 7.4+
March 31, 2020

Arrow Functions in PHP 7.4+

Posted on March 31, 2020  (Last modified on December 27, 2022 )
2 minutes  • 420 words
This project uses these versions of languages, frameworks, and libraries.
  • php php : 7.4
This tutorial may work with newer versions and possibly older versions, but has only been tested on the versions mentioned above.

PHP is finally getting arrow functions (also known as Short Closures)! I know, I know. I’m just as excited as you are. Here’s everything you need to know about arrow functions in PHP.

First, arrow functions are an approved RFC for PHP 7.4. If you’re using a version of PHP that’s lower than 7.4, you won’t be able to use arrow functions.

Second, arrow functions can only contain one single expression. The goal with arrow functions (as stated in the RFC ) is to help reduce verbosity and multi-line arrow functions are likely to cause more confusion than classic anonymous functions.

Writing an Arrow Function

Now with that out of the way, let’s write our first arrow function by converting a classic function to arrow syntax. We’ll use this example as our classic function:

var_dump(array_filter([1,2,3,4], function($value) {
  return $value % 2 === 0;
}));

If we run this, we’ll get some output like this:


array(2) {
  [1]=>
  int(2)
  [3]=>
  int(4)
}

Now, let’s take this “classic” style anonymous function and convert it over to an arrow function.

var_dump(array_filter([1,2,3,4], fn($value) => $value % 2 === 0));

Excellent. This feels way more concise and, in my opinion, readable. In PHP 7.4+, running this will yield:

array(2) {
  [1]=>
  int(2)
  [3]=>
  int(4)
}

You’ll notice that we can omit the return keyword using arrow functions. In fact, we HAVE to omit the return keyword when using arrow functions — It’s not allowed.

Using values from another scope

The BEST part about arrow functions, however, is that they have access to the scope that they’re being run within. This means that we can avoid the use keyword. Take this example which returns the elements in the first array that aren’t in the second:

function arrayDiff($a, $b) {
  return array_values(array_filter($a, function ($x) use ($b) { return !in_array($x, $b); }));
}

You’ll notice we’re using the use keyword to make $b available to our anonymous function. I really, really dislike the use keyword. I, personally, don’t feel like we should have to declare every contextual dependency to our function, but I understand why we’ve had to do this. However, with arrow functions, we no longer have to use the use keyword to make scoped variables available to our function.

Instead, we can write something like this:


function arrayDiff($a, $b) {
  return array_values(array_filter($a, fn($x) =>!in_array($x, $b)));
}

That’s so much better! Keep in mind that this feature also allows us to use $this in our arrow function!

Check out more of my PHP tips and tricks here!

Cartoon headshot of Brad Cypert
Follow me

Connect with me to follow along on my journey in my career, open source, and mentorship. Occasionally, I'll share good advice and content (quality not guaranteed).