PHP Arrow Functions: A Comparative Analysis with JavaScript Arrow Functions

The development of programming languages is a continual evolution, constantly refining syntax, adding features, and enhancing performance. The introduction of PHP 7.4 brought numerous significant updates, one of which is the addition of arrow functions (which I use on the regular, in JavaScript). This article takes a look at PHP arrow functions, how they compare to JavaScript arrow functions, and some ideal use cases for them.

PHP Arrow Functions

In PHP, short arrow functions provide a more concise syntax to create anonymous functions, enabling developers to write cleaner, more readable code.

Here’s a basic example of an arrow function in PHP:

$numbers = [1, 2, 3, 4, 5];
$squares = array_map(fn($n) => $n ** 2, $numbers);

This code takes an array of numbers and uses array_map with an arrow function to create a new array where each number is squared. The fn keyword is used to define an arrow function.

In the context of an arrow function, variables used inside the function refer to the value they have in their parent scope, this is called lexical scoping. For example:

$x = 10;
$addX = fn($n) => $n + $x;
echo $addX(5); // Outputs: 15

Arrow functions capture variables by value automatically, so if you were to write this in PHP without an arrow function, you would be required to capture $x by value like so:

$x = 10;
$addX = function($n) use ($x) {
    return $n + $x;
};
echo $addX(5); // Outputs: 15

As you can see, arrow functions provide some key use cases for simplicity in PHP, but let’s see how they compare to JavaScript arrow functions.

Comparing PHP and JavaScript Arrow Functions

JavaScript introduced arrow functions in ES6 as a way to write shorter function syntax. In many ways, arrow functions in JavaScript and PHP are similar. They both allow for more concise syntax and they both use lexical scoping.

Here’s a JavaScript equivalent of the previous PHP example:

let x = 10;
let addX = (n) => n + x;
console.log(addX(5)); // Outputs: 15

However, there are a few key differences:

  1. Syntax: In JavaScript, arrow functions are denoted using the => symbol only, whereas in PHP, the fn keyword is used alongside =>.
  2. Handling of this: In JavaScript, arrow functions do not bind their own this. Instead, this is looked up in the scope chain. In contrast, PHP does not have a this context in the same way, so this isn’t a consideration for PHP arrow functions.
  3. Use of parentheses: In JavaScript, if there’s only one parameter, the parentheses around the parameter can be omitted. In PHP, the parentheses are mandatory, even if there’s only one parameter.

Best Use Cases for Arrow Functions in PHP

Arrow functions are most useful in PHP when you need a small, self-contained piece of functionality that you can pass around—such as a callback function in array methods like array_map, array_filter, and array_reduce.

Mapping an array

$numbers = [1, 2, 3, 4, 5]; 
$doubled = array_map(fn($n) => $n * 2, $numbers);

Filtering an array

$numbers = [1, 2, 3, 4, 5];
$evens = array_filter($numbers, fn($n) => $n % 2 === 0);

Reducing an array

$numbers = [1, 2, 3, 4, 5]; 
$sum = array_reduce($numbers, fn($carry, $n) => $carry + $n, 0);

Sorting an array

$numbers = [5, 2, 1, 4, 3]; 
usort($numbers, fn($a, $b) => $a <=> $b);

In each of these examples, the arrow function provides a clean, concise way to define the functionality needed for the operation.

In conclusion, the introduction of arrow functions in PHP has enhanced the language’s functionality, making it more robust and flexible. Although these arrow functions are quite similar to those in JavaScript, they have their unique characteristics and benefits. They are a great tool for PHP developers, allowing for more concise, readable, and maintainable code.