Arrow Functions

Arrow functions were introduced in PHP 7.4 as a more concise syntax for anonymous functions.

Both anonymous functions and arrow functions are implemented using the Closure class.

Arrow functions have the basic form fn (argument_list) => expr.

Arrow functions support the same features as anonymous functions, except that using variables from the parent scope is always automatic.

When a variable used in the expression is defined in the parent scope it will be implicitly captured by-value. In the following example, the functions $fn1 and $fn2 behave the same way.

Example #1 Arrow functions capture variables by value automatically


$fn1 fn($x) => $x $y;
// equivalent to using $y by value:
$fn2 = function ($x) use ($y) {
$x $y;


The above example will output:


This also works if the arrow functions are nested:

Example #2 Arrow functions capture variables by value automatically, even when nested


$fn fn($x) => fn($y) => $x $y $z;
// Outputs 51

Similarly to anonymous functions, the arrow function syntax allows arbitrary function signatures, including parameter and return types, default values, variadics, as well as by-reference passing and returning. All of the following are valid examples of arrow functions:

Example #3 Examples of arrow functions


(array $x) => $x;
fn(): int => $x;
fn($x 42) => $x;
fn(&$x) => $x;
fn&($x) => $x;
fn($x, ...$rest) => $rest;


Arrow functions use by-value variable binding. This is roughly equivalent to performing a use($x) for every variable $x used inside the arrow function. A by-value binding means that it is not possible to modify any values from the outer scope. Anonymous functions can be used instead for by-ref bindings.

Example #4 Values from the outer scope cannot be modified by arrow functions


$fn fn() => $x++; // Has no effect
var_export($x);  // Outputs 1



Version Description
7.4.0 Arrow functions became available.


Note: It is possible to use func_num_args(), func_get_arg(), and func_get_args() from within an arrow function.

Here you can write a comment

Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Total Commander 11.00 (final) is available! What's new?

Total Commander, a popular file manager, has recently released its latest version, 11.00. With a variety of new features and corrections, this version offers users an enhanced and optimized experience. ...


Autor : admin
Category: Software-Updates

Which technologies should a PHP programmer master?

In today's digital world, the role of a PHP programmer is of great importance. ...


Autor : admin
Category: Miscellaneous

PHPUnit version 10 released

PHPUnit version 10 released

Version 10 of the popular test framework PHPUnit has been released and is available for download. ...


Autor : TheMax
Category: Software & Web-Development

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial