PHP added arrow functions in version 7.4. They make code shorter. Arrow functions can use variables from the same scope without extra setup.
You don’t need to pass anything by reference or use the global
keyword.
We will cover the following topics in this article:
- Understand how arrow functions (
fn
) work. - When and why to use arrow functions in PHP?
- Lexical scoping clarity.
- Examples.
Let’s start with how they work.
Understand How Arrow Functions (fn
) Work in PHP
PHP arrow functions give you a shorter way to write simple or repeated functions. You can store them in a variable and call them through that variable.
Here’s the basic syntax:
$func = fn() => VALUES_TO_RETURN;
This syntax uses a short (arrow) function. It skips the function
keyword and curly braces. The fn()
defines the arrow function, and =>
returns the value directly—no need to use the return
keyword.
Arrow functions and traditional anonymous functions both let you create inline functions, but they handle variables from the outer scope differently.
Traditional Anonymous Function:
Here, you must explicitly list external variables with use
.
$factor = 2;
$multiply = function ($n) use ($factor) {
return $n * $factor;
};
echo $multiply(10);
Output:
20
Arrow Function:
It automatically uses variables from the parent scope. No need to use the use
keyword.
$factor = 2;
$multiply = fn($n) => $n * $factor;
echo $multiply(30);
Output:
60
Here are the key differences for each one
Arrow functions:
- They always return the result—no
return
keyword needed. - They capture variables by value automatically.
Anonymous functions:
- You need to use the
use (...)
keyword to access outer variables. - Arrow syntax is shorter.
The main benefit? It lets you write shorter code. For example, when you need a callback—like with array functions such as array_filter
.
Here is an example:
Traditional anonymous function:
$numbers = [1, 2, 3, 4, 5];
$square = array_map(function ($n) {
return $n * $n;
}, $numbers);
print_r($square );
Output:
Array
(
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
Arrow function version:
$numbers = [1, 2, 3, 4, 5];
$square = array_map(fn($n) => $n * $n, $numbers);
The output of this example will be the same as the previous one.
Here’s another example that shows how to use an arrow function with array_filter.
$numbers = [1, 2, 3, 4, 5];
$even = array_filter($numbers, fn($n) => $n % 2 === 0);
print_r($even);
Output:
Array
(
[1] => 2
[3] => 4
)
This code filters even numbers from an array with an arrow function. It keeps values where $n % 2 === 0
. Output: only even numbers.
So, when and why to use them? Let’s answer this question in the following section.
When and Why to Use Arrow Functions in PHP?
Use arrow functions when you do small tasks. They work best in callbacks or simple operations. That happens when you use array functions such as
array_map
array_filter
array_reduce
.
Here are why you have to use them:
- It helps you write short code.
- No need for
use
keyword to access global scope variables. - Automatic return saves space.
- It is readable and it has short functions.
When to use them:
- Inline functions that do one thing.
- Short logic in closures.
- Situations where readability matters.
- Callbacks inside array operations.
Lexical Scoping Clarity
Arrow functions handle scope differently from traditional anonymous functions. They inherit the outer scope directly. You don’t need the use
keyword.
Here is an example:
$multiplier = 10;
$calculate = function ($value) use ($multiplier) {
return $value * $multiplier;
};
$arrowCalculate = fn($value) => $value * $multiplier;
print_r( $arrowCalculate(55) );
The output:
550
In a regular function, the use
keyword pulls in $multiplier
. In an arrow function, it’s included automatically.
The arrow functions only allow one expression. No blocks, no extra lines.
Examples
Print a message with an arrow function:
$uname = "Montasser";
$greet = fn() => "Hello, $uname!";
echo $greet();
The arrow function uses a variable from the outer scope ($uname
). You don’t need to pass it or use use
.
This works because arrow functions automatically inherit from the surrounding scope. It’s useful for short messages or responses.
Discount calculator:
$discount = 15;
$applyDiscount = fn($price) => $price - ($price * $discount / 100);
echo $applyDiscount(200);
The 15% is a discount. In this function, we used the $discount
from outside. No extra code needed. Arrow functions are good for this kind of logic where you apply a formula.
Here is how to sort with a custom rule:
$names = ["john", "Alice", "bob"];
usort($names, fn($a, $b) => strtolower($a) <=> strtolower($b));
print_r($names);
The arrow function tells usort
how to compare the names. It converts both to lowercase, so sorting ignores case. This keeps the sorting logic short and inside the usort
call.
Use in a loop with an external variable:
$bonus = 50;
$salaries = [1000, 1200, 1500];
$withBonus = array_map(fn($salary) => $salary + $bonus, $salaries);
print_r($withBonus);
Arrow function adds the same bonus to each salary. It uses $bonus
from outside. No need for use
. This shows how arrow functions keep your loop logic cleaner and shorter.
Wrapping Up
You learned in this tutorial how PHP arrow functions work and how they handle scope without extra setup. You also saw when to use them and how they make your code readable.
Here’s a quick recap:
- Arrow functions use
fn
and need only one expression. - They inherit variables from the outer scope automatically.
- You don’t need
use
orglobal
. - They return values without writing
return
. - Avoid them for anything that needs multiple lines or complex logic.
FAQ’s
Can arrow functions replace all anonymous functions?
Do arrow functions affect performance?
Can arrow functions have multiple lines?
Can arrow functions access global variables?
Can arrow functions be recursive?
Can I use multiple parameters in an arrow function?
$sum = fn($a, $b, $c) => $a + $b + $c;