Modern JS for PHP Devs

Arrow functions coming soon to PHP 7.4

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

If arrow functions still look like hieroglyphics to you, I can thoroughly recommend Wes Bos’ Beginner JavaScript and ES6+ For Everyone courses.

They will really help you to grok and enjoy modern JS syntax.

Please note: The above are affiliate links.

Chris Colborne

Written by Chris Colborne, an Aussie web dev from Brisbane, Australia. You should follow him on Twitter

More posts

Blog comments powered by Disqus.