<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ web developers - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ web developers - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 27 Jul 2026 22:42:15 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/web-developers/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Use Arrow Functions in PHP 7.4+ ]]>
                </title>
                <description>
                    <![CDATA[ Arrow functions were introduced in PHP 7.4 to allow devs to write short, anonymous functions. They offer a compact alternative to traditional closures, especially when the function body is small and focused. In this article, you will learn how to use... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-arrow-functions-in-php/</link>
                <guid isPermaLink="false">681cf9062cbe43bee0f4138f</guid>
                
                    <category>
                        <![CDATA[ PHP ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ web developers ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Montasser Mossallem ]]>
                </dc:creator>
                <pubDate>Thu, 08 May 2025 18:33:42 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1746354775446/092005ae-12bb-4be9-a72c-aacc5f044962.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Arrow functions were introduced in PHP 7.4 to allow devs to write short, anonymous functions. They offer a compact alternative to traditional closures, especially when the function body is small and focused.</p>
<p>In this article, you will learn how to use the arrow function in PHP with examples. You’ll also learn about the difference between arrow functions and anonymous functions.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#prerequisites">Prerequisites</a></p>
</li>
<li><p><a class="post-section-overview" href="#understand-the-arrow-functions-in-php">Understand the Arrow Functions in PHP</a></p>
</li>
<li><p><a class="post-section-overview" href="#the-difference-between-arrow-functions-and-anonymous-functions-in-php">The Difference Between Arrow Functions and Anonymous Functions in PHP</a></p>
<ul>
<li><p><a class="post-section-overview" href="#1-syntax">1. Syntax</a></p>
</li>
<li><p><a class="post-section-overview" href="#2-variable-scope-lexical-scope">2. Variable Scope (Lexical Scope)</a></p>
</li>
<li><p><a class="post-section-overview" href="#3-readability-and-brevity">3. Readability and Brevity</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#how-to-return-arrow-functions-from-other-functions">How to Return Arrow Functions from Other Functions</a></p>
</li>
<li><p><a class="post-section-overview" href="#how-to-use-arrow-functions-in-your-code">How to Use Arrow Functions in Your Code?</a></p>
<ul>
<li><p><a class="post-section-overview" href="#use-the-arrow-function-within-array_map">Use the Arrow Function within <code>array_map()</code></a></p>
</li>
<li><p><a class="post-section-overview" href="#use-the-arrow-function-with-array_filter">Use the Arrow Function with <code>array_filter()</code></a></p>
</li>
<li><p><a class="post-section-overview" href="#use-the-arrow-function-with-array_reduce">Use the arrow function with <code>array_reduce()</code></a></p>
</li>
<li><p><a class="post-section-overview" href="#nest-arrow-functions-in-php">Nest arrow functions in PHP</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#wrapping-up">Wrapping Up</a></p>
</li>
</ul>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>You should know how to write basic PHP code, such as functions, and be able to work with arrays. Make sure you’re using PHP 7.4 or newer because arrow functions only work in that version and above.</p>
<h2 id="heading-understanding-arrow-functions-in-php">Understanding Arrow Functions in PHP</h2>
<p>The <a target="_blank" href="https://flatcoding.com/tutorials/php/arrow-functions/">PHP arrow function</a> is a shorthand syntax. It defines an anonymous function and is designed for simple operations and expressions.</p>
<p>Arrow functions in PHP are best used when:</p>
<ul>
<li><p>You need a quick callback or inline function.</p>
</li>
<li><p>The function returns a single expression.</p>
</li>
<li><p>You want to avoid repetitive <code>use</code> statements.</p>
</li>
</ul>
<p>The basic syntax of an arrow function is:</p>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">parameter_list</span>) =&gt; <span class="hljs-title">expression</span></span>;
</code></pre>
<ul>
<li><p><code>fn</code> is the keyword that defines the arrow function.</p>
</li>
<li><p><code>parameter_list</code> is the list of parameters (similar to a normal function).</p>
</li>
<li><p><code>=&gt;</code> separates the parameter list from the expression.</p>
</li>
<li><p><code>expression</code> is the value the function returns. You cannot use a block of statements here – only a single expression is allowed.</p>
</li>
</ul>
<p>Arrow functions automatically capture variables from the scope. They don’t need the <code>use</code> keyword as shown below:</p>
<pre><code class="lang-php">$var_name = <span class="hljs-number">10</span>; 
$func = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">$n</span>) <span class="hljs-title">use</span> (<span class="hljs-params"> $var_name </span>) </span>{
   <span class="hljs-keyword">return</span> $n * $var_name;
}
</code></pre>
<p>You can use the variables in the scope directly:</p>
<pre><code class="lang-php">$var_name = <span class="hljs-number">10</span>; 
$func = <span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">$n</span>) =&gt; $<span class="hljs-title">n</span> * $<span class="hljs-title">var_name</span></span>;
</code></pre>
<p>Here’s a lexical scoping example:</p>
<pre><code class="lang-php">$multiplier = <span class="hljs-number">3</span>;
$multiply = <span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">$x</span>) =&gt; $<span class="hljs-title">x</span> * $<span class="hljs-title">multiplier</span></span>;
<span class="hljs-keyword">echo</span> $multiply(<span class="hljs-number">4</span>); <span class="hljs-comment">// Outputs: 12</span>
</code></pre>
<p>The variable <code>$multiplier</code> is automatically captured from the outer scope. You don’t need to use <code>use($multiplier)</code> as you would in a traditional anonymous function.</p>
<p>Key rules of arrow function syntax:</p>
<ul>
<li><p>Always use <code>fn</code>, not <code>function</code>.</p>
</li>
<li><p>No curly braces or <code>return</code> keyword – just a single expression.</p>
</li>
<li><p>Automatic variable capture from the outer scope.</p>
</li>
<li><p>It cannot contain multiple statements or control structures (like <code>if</code>, <code>foreach</code>, and so on).</p>
</li>
</ul>
<p>Let’s move on to the following section to take a look at the difference between arrow functions and <a target="_blank" href="https://flatcoding.com/tutorials/php-programming/php-anonymous-functions/">anonymous functions in PHP</a>.</p>
<h2 id="heading-the-difference-between-arrow-functions-and-anonymous-functions-in-php">The Difference Between Arrow Functions and Anonymous Functions in PHP</h2>
<p>PHP supports two types of anonymous functions (that is, functions without a name):</p>
<ul>
<li><p><strong>Traditional anonymous functions</strong> are defined by the <code>function</code> keyword</p>
</li>
<li><p><strong>Arrow functions</strong> are introduced in PHP 7.4 within the <code>fn</code> keyword</p>
</li>
</ul>
<p>Both types can be assigned to variables and used for callbacks or as function arguments. They serve similar purposes, but differ in syntax and how they handle external variables.</p>
<p>Let’s look at their key differences.</p>
<h3 id="heading-1-syntax">1. Syntax</h3>
<h4 id="heading-arrow-function">Arrow Function:</h4>
<p>Arrow functions use a single-line expression without braces or a <code>return</code> statement.</p>
<pre><code class="lang-php">$square = <span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">$n</span>) =&gt; $<span class="hljs-title">n</span> * $<span class="hljs-title">n</span></span>;
</code></pre>
<p>The arrow function assigns it to the variable <code>$square</code>. The function takes one parameter, <code>$n</code>, and returns <code>$n * $n</code> (the square of <code>$n</code>).</p>
<h4 id="heading-anonymous-function">Anonymous Function:</h4>
<pre><code class="lang-php">$square = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">$n</span>) </span>{
    <span class="hljs-keyword">return</span> $n * $n;
};
</code></pre>
<p>Anonymous functions use a full function block and require an explicit <code>return</code>. They’re used for multi-line logic or complex behavior.</p>
<h3 id="heading-2-variable-scope-lexical-scope">2. Variable Scope (Lexical Scope)</h3>
<p>Arrow functions automatically capture variables from the outer scope:</p>
<pre><code class="lang-php">$factor = <span class="hljs-number">2</span>;
$multiply = <span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">$x</span>) =&gt; $<span class="hljs-title">x</span> * $<span class="hljs-title">factor</span></span>;
</code></pre>
<p>Anonymous functions require you to manually import external variables using <code>use</code>:</p>
<pre><code class="lang-php">$factor = <span class="hljs-number">2</span>;
$multiply = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">$x</span>) <span class="hljs-title">use</span> (<span class="hljs-params">$factor</span>) </span>{
    <span class="hljs-keyword">return</span> $x * $factor;
};
</code></pre>
<p>You cannot use the variable in the scope within the anonymous function unless you use the <code>use</code> keyword.</p>
<h3 id="heading-3-readability-and-brevity">3. Readability and Brevity</h3>
<p>Arrow functions are shorter. They help you write small and single-expression callbacks:</p>
<pre><code class="lang-php">$numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];
$squares = array_map(<span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">$n</span>) =&gt; $<span class="hljs-title">n</span> * $<span class="hljs-title">n</span>, $<span class="hljs-title">numbers</span>)</span>;
</code></pre>
<p>But anonymous functions are better when:</p>
<ul>
<li><p>The function body has multiple lines.</p>
</li>
<li><p>You need complex logic or control structures.</p>
</li>
</ul>
<p><strong>Here is a table that shows you the key differences:</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>Arrow Function</td><td>Anonymous Function</td></tr>
</thead>
<tbody>
<tr>
<td>Introduced in</td><td>PHP 7.4</td><td>PHP 5.3</td></tr>
<tr>
<td>Syntax</td><td>Short, single-expression</td><td>Verbose, full-function body</td></tr>
<tr>
<td>Scope handling</td><td>Automatic (lexical)</td><td>Manual (<code>use</code>) keyword</td></tr>
<tr>
<td>Multiline body</td><td>Not allowed</td><td>Allowed</td></tr>
<tr>
<td>Return keyword</td><td>Not used</td><td>Required</td></tr>
</tbody>
</table>
</div><p>Let’s move on to the section below to understand how to return an arrow function from another function.</p>
<h2 id="heading-how-to-return-arrow-functions-from-other-functions">How to Return Arrow Functions from Other Functions</h2>
<p>Functions are first-class citizens. This means you can return a function from another function. That includes arrow functions.</p>
<p>You can define and return an arrow function from within a regular function like this:</p>
<pre><code class="lang-php"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getMultiplier</span>(<span class="hljs-params">$factor</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">$x</span>) =&gt; $<span class="hljs-title">x</span> * $<span class="hljs-title">factor</span></span>;
}

$double = getMultiplier(<span class="hljs-number">2</span>);
<span class="hljs-keyword">echo</span> $double(<span class="hljs-number">5</span>); <span class="hljs-comment">// Outputs: 10</span>
</code></pre>
<p>In this example:</p>
<ul>
<li><p><code>getMultiplier()</code> returns an arrow function.</p>
</li>
<li><p>The arrow function captures <code>$factor</code> from the outer scope automatically (lexical scoping).</p>
</li>
<li><p>The returned function can be stored in a variable and used like any other callable.</p>
</li>
</ul>
<p>It lets you generate small functions based on parameters and reduces code repetition.</p>
<p>Use this syntax when you need to build dynamic behavior – like custom filters or function factories.</p>
<p>Let’s move on to the section below to see how you can use arrow functions in your code.</p>
<h2 id="heading-how-to-use-arrow-functions-in-your-code">How to Use Arrow Functions in Your Code</h2>
<h3 id="heading-use-the-arrow-function-within-arraymap">Use the Arrow Function within <code>array_map()</code>:</h3>
<p><code>array_map()</code> lets you set a callback to each element of an array. It allows you to define the callback directly within the function call.</p>
<p>Example:</p>
<pre><code class="lang-php">$numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];

$squares = array_map(<span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">$n</span>) =&gt; $<span class="hljs-title">n</span> * $<span class="hljs-title">n</span>, $<span class="hljs-title">numbers</span>)</span>;

print_r($squares);
<span class="hljs-comment">// Outputs: [1, 4, 9, 16, 25]</span>
</code></pre>
<p>The arrow function <code>fn($n) =&gt; $n * $n</code> is executed for each element of the <code>$numbers</code> array. The result is a new array of squared values.</p>
<h3 id="heading-use-the-arrow-function-with-arrayfilter">Use the Arrow Function with <code>array_filter()</code></h3>
<p><code>array_filter()</code> filters elements of an array within a callback. Arrow functions define a short filter condition inline.</p>
<p>Example:</p>
<pre><code class="lang-php">$numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>];

$evenNumbers = array_filter($numbers, <span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">$n</span>) =&gt; $<span class="hljs-title">n</span> % 2 === 0)</span>;

print_r($evenNumbers);
<span class="hljs-comment">// Outputs: [2, 4, 6]</span>
</code></pre>
<p>Here, the arrow function checks if each number is even. The result is an array that contains only the even numbers.</p>
<h3 id="heading-use-the-arrow-function-with-arrayreduce">Use the arrow function with <code>array_reduce()</code></h3>
<p><code>array_reduce()</code> reduces an array to a single value based on a callback function. Arrow functions help make the code compact.</p>
<p>Example:</p>
<pre><code class="lang-php">$numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];

$sum = array_reduce($numbers, <span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">$carry, $n</span>) =&gt; $<span class="hljs-title">carry</span> + $<span class="hljs-title">n</span>, 0)</span>;

<span class="hljs-keyword">echo</span> $sum; <span class="hljs-comment">// Outputs: 15</span>
</code></pre>
<p>The arrow function adds each number in the array. <code>$carry</code> holds the running total and <code>$n</code> is the current number.</p>
<h3 id="heading-nest-arrow-functions-in-php">Nest arrow functions in PHP</h3>
<p>Here the inner function performs one operation and the outer function processes the results of the inner function.</p>
<pre><code class="lang-php">$numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];

$doubleAndSquare = array_map(
    <span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">$n</span>) =&gt; <span class="hljs-title">fn</span>(<span class="hljs-params">$x</span>) =&gt; (<span class="hljs-params">$x * <span class="hljs-number">2</span></span>) ** 2,  
    $<span class="hljs-title">numbers</span>
)</span>;

$results = array_map(
    <span class="hljs-function"><span class="hljs-keyword">fn</span>(<span class="hljs-params">$fn</span>) =&gt; $<span class="hljs-title">fn</span>(<span class="hljs-params"><span class="hljs-number">3</span></span>),  
    $<span class="hljs-title">doubleAndSquare</span>
)</span>;

print_r($results);
<span class="hljs-comment">// Outputs: [36, 36, 36, 36, 36]</span>
</code></pre>
<p>In the above code, the first <code>array_map()</code> creates a list of arrow functions that double and then square the number. Each element in the <code>$numbers</code> array gets mapped to a nested arrow function.</p>
<p>The second <code>array_map()</code> applies the inner arrow function (which doubles and squares the value) to the number <code>3</code>. It results in an array of the same result.</p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>In this article, you’ve learned the basic features and syntax of arrow functions. It shows you their advantages over anonymous functions.</p>
<p>Here are some key takeaways:</p>
<ol>
<li><p>Arrow functions were introduced in PHP 7.4. They provide you with a new syntax to define anonymous functions with simpler code.</p>
</li>
<li><p>Arrow functions are a shorter way to write anonymous functions. They use one line of code and don’t need curly braces or the <code>return</code> keyword.</p>
</li>
<li><p>Arrow functions automatically get variables from scope. This allows you to use an arrow function as a callback in functions like <code>array_map()</code> or <code>array_filter()</code>.</p>
</li>
</ol>
<p>Resources:</p>
<ul>
<li><p><a target="_blank" href="https://www.php.net/manual/en/functions.arrow.php">PHP docs on arrow functions</a></p>
</li>
<li><p><a target="_blank" href="https://flatcoding.com/">Flatcoding blog</a> where I publish many other tutorials</p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
