PHP Arrow Functions (original) (raw)

Last Updated : 15 Apr, 2025

In the development, developers used to face problems like writing long code for simple tasks, needing to manually capture variables with use, and having less readable functions. PHP arrow functions solve this by providing a shorter syntax, automatically capturing variables from the parent scope, and making the code cleaner, simpler, and easier to understand.

In this article, we will discuss PHP arrow functions and how they differ from regular anonymous functions.

What are PHP Arrow Functions?

PHP arrow functions are a shorthand syntax for anonymous functions. PHP was introduced in PHP 7.4, which provides a shorter way to write anonymous functions. They allow you to create functions with fewer lines of code while maintaining functionality. They provide an easy-to-read syntax, particularly for small, concise operations.

Both arrow functions and regular anonymous functions are implemented using the Closure class in PHP. The Closure class represents a function and its enclosed environment, allowing access to variables from the surrounding scope.

**Syntax:

fn (argument_list) => expr

**In this syntax:

**Now, let us understand with the help of the example:

PHP `

add=fn(add = fn(add=fn(a, b)=>b) => b)=>a + $b; echo $add(3, 4); ?>

`

**In this example:

Key Features of PHP Arrow Functions

Arrow Functions Capture Variables by Value Automatically

The feature of PHP arrow functions is that they automatically capture variables from the parent scope by value. This means that when a variable is used inside an arrow function, it is implicitly captured by its value from the surrounding scope.

You do not need to explicitly use the use keyword as you would in regular anonymous functions.

**Now, let us understand with the help of the example:

PHP `

y=1;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>n</mi><mn>1</mn><mo>=</mo><mi>f</mi><mi>n</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">fn1=fn(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal">n</span><spanclass="mord">1</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal">n</span><spanclass="mopen">(</span></span></span></span>x)=>y = 1; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>n</mi><mn>1</mn><mo>=</mo><mi>f</mi><mi>n</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">fn1 = fn(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">n</span><span class="mord">1</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">n</span><span class="mopen">(</span></span></span></span>x) => y=1;<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>n</mi><mn>1</mn><mo>=</mo><mi>f</mi><mi>n</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">fn1=fn(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;verticalalign:0.1944em;"></span><spanclass="mordmathnormal"style="marginright:0.10764em;">f</span><spanclass="mordmathnormal">n</span><spanclass="mord">1</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal"style="marginright:0.10764em;">f</span><spanclass="mordmathnormal">n</span><spanclass="mopen">(</span></span></span></span>x)=>x + y;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>n</mi><mn>2</mn><mo>=</mo><mi>f</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">fn2=function(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal">n</span><spanclass="mord">2</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">c</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">n</span><spanclass="mopen">(</span></span></span></span>x)use(y; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>n</mi><mn>2</mn><mo>=</mo><mi>f</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">fn2 = function (</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">n</span><span class="mord">2</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">u</span><span class="mord mathnormal">n</span><span class="mord mathnormal">c</span><span class="mord mathnormal">t</span><span class="mord mathnormal">i</span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mopen">(</span></span></span></span>x) use (y;<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>n</mi><mn>2</mn><mo>=</mo><mi>f</mi><mi>u</mi><mi>n</mi><mi>c</mi><mi>t</mi><mi>i</mi><mi>o</mi><mi>n</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">fn2=function(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;verticalalign:0.1944em;"></span><spanclass="mordmathnormal"style="marginright:0.10764em;">f</span><spanclass="mordmathnormal">n</span><spanclass="mord">2</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal"style="marginright:0.10764em;">f</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">c</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">n</span><spanclass="mopen">(</span></span></span></span>x)use(y) { return x+x + x+y; }; var_export($fn1(3)); echo "\n"; var_export($fn2(4)) ?>

`

**In this example:

Arrow Functions Capture Variables by Value Automatically, Even When Nested

Arrow functions in PHP not only capture variables from the parent scope, but they also capture them by value automatically, even when the functions are nested. This behavior makes it easier to work with nested functions without needing additional syntax to pass variables between them.

**Now, let us understand with the help of the example:

PHP `

z=3;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>n</mi><mo>=</mo><mi>f</mi><mi>n</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">fn=fn(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal">n</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal">n</span><spanclass="mopen">(</span></span></span></span>x)=>fn(z = 3; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>n</mi><mo>=</mo><mi>f</mi><mi>n</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">fn = fn(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">n</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">n</span><span class="mopen">(</span></span></span></span>x) => fn(z=3;<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>n</mi><mo>=</mo><mi>f</mi><mi>n</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">fn=fn(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;verticalalign:0.1944em;"></span><spanclass="mordmathnormal"style="marginright:0.10764em;">f</span><spanclass="mordmathnormal">n</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal"style="marginright:0.10764em;">f</span><spanclass="mordmathnormal">n</span><spanclass="mopen">(</span></span></span></span>x)=>fn(y) => x∗x * xy + $z; var_export($fn(4)(10)); ?>

`

**In this example:

Values from the Outer Scope Cannot Be Modified by Arrow Functions

While arrow functions can automatically capture variables from the outer scope by value, they cannot modify those captured values. This is an important difference between arrow functions and regular anonymous functions, which can modify variables from the outer scope (if passed by reference).

**Now, let us understand with the help of the example:

PHP `

fn=fn()=>fn = fn() => fn=fn()=>x--; // Has no effect $fn(); var_export($x); // Outputs 10 ?>

`

**In this example:

Benefits of Using PHP Arrow Functions

Limitations of PHP Arrow Functions

Arrow Functions vs Anonymous Functions

Below are the differences between arrow functions and anonymous functions.

Arrow Functions Anonymous Function
fn (arguments) => expression; function (arguments) { /* statements */ }
Automatically captures variables from the parent scope by value. No need for use. Requires the use keyword to capture variables from the parent scope.
Ideal for short, single-expression operations. Suitable for complex logic and multiple statements.