The first appearance of PHP arrow functions were in PHP version 7.4, the arrow function can access all variables in the same scope, so you don’t need to define an argument as in PHP anonymous functions.

Furthermore, there is no requirement to pass it as a reference or utilize a global keyword.

The PHP functions are a shortened version of anonymous functions, providing a concise alternative for coding repetitive or small-scale functions. Which can be stored in the variable and access it in the same time by its variable.

The PHP arrow function expression would be written as the below syntax.

$func = fn() => VALUES TO RETUEN HERE

So the fn word refers to function and double braces are for the arguments. But the arrow or these two operators “=” and “>” to write the return expression.

The equivalent of the PHP arrow function can be written as the following anonymous function.

$func = function( ARGS ) {
  return RESULT

Let’s see examples.

PHP Arrow Functions Examples

In the following example, I will pass the arrow function as an argument to another PHP callback.


$fn = fn($vr) => $vr > 10;

$list = array_filter([
], $fn);


The output would be like the following result:

Array ( [0] => 11 [1] => 22 [2] => 33 [3] => 62 [5] => 44 [7] => 22 [8] => 55 )

The following example, will show you how to multiply two numbers by using the arrow function. In this example, the final result will be in the return expression.


$fn     = fn($v, $m) => $v * $m;
$result = $fn(10, 100);
echo $result; // 1000 

But sometimes you need to pass the variables into the arrow function without arguments, which means using all PHP variables that already created out of the function. Let’s see that in the following section.

Accessing the Variables in the Scopes in PHP Arrow Functions

The PHP arrow functions allow you to call the variables inside the functions without any problem. For example.


$xyz = 5;
$fat = fn() => $xyz * 10;
echo $fat(); // 50 

In this example, PHP arrow function reached to the outside variable without using the global word or passing it as an argument.

But, the PHP arrow function will not affect on the scope variables which mean, it will not change the variable value.

For example.

$z = 5;
$fn = fn() => $z++;
echo $z; // 5

So, it will print 5 and not 6.

Nested Arrow Functions

The nested arrow function means an arrow function inside an arrow function, the parent function is depending on the child function that already created inside it. Let’s see an example.


$fn = fn($x) => fn($z) => $x * $z;
$z = $fn(2)(20);
echo $z; // 40

So, if you look at the first function, you will see it will return a result but when it goes to return this expression it will find another block that already has another expression to return. Let’s see how it works.

The calling part, there are two callable functions, which are. $fn(2) and (20). These two both are working together dynamically. So the first one passes the $x variable and the second one passes the $z variable.

So, in the result should multiply the two numbers together $x * $z and as you know the arrow function can work with any variable in the scope.

Wrapping Up

In this article, you understood what the arrow function is and how to use it. Also, you saw many examples such as call a variable in the scope, nested arrow function, and use it as a callback within another function callback.