Chris Colborne

Arrow functions coming soon to PHP 7.4

September 08, 2019

Photo by Stephan Henning on Unsplash

The latest version of PHP 7.4 is coming soon. Release candidate 1 (the first of 6 planned RCs) was just released on 5th September and 7.4 is scheduled for General Availability on November 28, 2019.

If you’re like me, one of the cooler additions to look forward to coming to the language is arrow functions.

Arrow functions in ES6

If you’ve done any modern JavaScript, you’re probably familiar with arrow functions which came in ES6. In case you’re not, they look like this:

// in JavaScript arrow functions looks like this

// add numbers and return the result
const addSomeNumbers = (a, b) => a + b;

// echo out a message and return the input parameter
const doTheThing = theThing => {
  console.log(`I'm doing ${theThing}`);
  return theThing;

Arrow functions in PHP

  • prefix with fn
  • only single statement implicit return style arrow functions are implemented currently, no block based style like the first JavaScript example (although the block style is in the future scope - named multi-statement bodies in the RFC)
  • supports implicit variable binding, in other words, use is not required

Here are the examples from the RFC showing the arrow syntax and implicit variable binding:

// in PHP arrow functions look like this

// long version required currently
function array_values_from_keys($arr, $keys) {
    return array_map(
      function ($x) use ($arr) { return $arr[$x]; }, // long

// with arrow functions
function array_values_from_keys($arr, $keys) {
    return array_map(
      fn($x) => $arr[$x], // short

// note the difference
$long  = function ($x) use ($arr) { return $arr[$x]; };
$short = fn($x) => $arr[$x];

Try it out

I’ve created a simple test repo so you can try them out now.

Check out

Like what you've read?

Why not subscribe to my mailing list to get my latest articles by email.

I respect the privacy of your email address. You can unsubscribe at any time.

Written by Chris Colborne, an Aussie software engineer from Brisbane, Australia. Follow me on Twitter

Chris Colborne © 2023