{"id":783,"date":"2021-03-27T12:11:29","date_gmt":"2021-03-27T12:11:29","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=783"},"modified":"2025-04-06T07:11:56","modified_gmt":"2025-04-06T07:11:56","slug":"php-arrow-functions","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-arrow-functions\/","title":{"rendered":"PHP Arrow Functions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about PHP arrow functions and how to use them effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-php-arrow-functions'>Introduction to PHP arrow functions <a href=\"#introduction-to-php-arrow-functions\" class=\"anchor\" id=\"introduction-to-php-arrow-functions\" title=\"Anchor for Introduction to PHP arrow functions\">#<\/a><\/h2>\n\n\n\n<p>PHP 7.4 introduced arrow functions that provide a more concise syntax for the <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-anonymous-functions\/\">anonymous functions<\/a>.<\/p>\n\n\n\n<p>The following illustrates the basic syntax for arrow functions:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">fn (arguments) =&gt; expression;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, an arrow function:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Starts with the <code>fn<\/code> keyword.<\/li>\n\n\n\n<li>Can have only one expression and return this expression.<\/li>\n<\/ul>\n\n\n\n<p>The arrow function is functionally equivalent to the following anonymous function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><span class=\"hljs-params\">(arguments)<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> expression; }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Unlike anonymous functions, arrow functions can access variables from their parent scopes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='assigning-an-arrow-function-to-a-variable'>Assigning an arrow function to a variable <a href=\"#assigning-an-arrow-function-to-a-variable\" class=\"anchor\" id=\"assigning-an-arrow-function-to-a-variable\" title=\"Anchor for Assigning an arrow function to a variable\">#<\/a><\/h2>\n\n\n\n<p>The following example illustrates how to assign an arrow function to a variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$eq = fn ($x, $y) =&gt; $x == $y;\n\n<span class=\"hljs-keyword\">echo<\/span> $eq(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-string\">'100'<\/span>); <span class=\"hljs-comment\">\/\/ 1 (or true)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRlcSA9IGZuICgkeCwgJHkpID0-ICR4ID09ICR5OwoKZWNobyAkZXEoMTAwLCAnMTAwJyk7IC8vIDEgKG9yIHRydWUp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define an arrow function and assign it to the $eq variable. The arrow function returns true if the two arguments are equal.<\/li>\n\n\n\n<li>Second, call the arrow function via the $eq variable<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='passing-an-arrow-function-to-a-function-example'>Passing an arrow function to a function example <a href=\"#passing-an-arrow-function-to-a-function-example\" class=\"anchor\" id=\"passing-an-arrow-function-to-a-function-example\" title=\"Anchor for Passing an arrow function to a function example\">#<\/a><\/h2>\n\n\n\n<p>The following example shows how to pass an arrow function to the <code>array_map()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$list = &#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>];\n\n$results = array_map(\n\tfn ($item) =&gt; $item * <span class=\"hljs-number\">2<\/span>,\n\t$list\n);\n\nprint_r($results);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRsaXN0ID0gWzEwLCAyMCwgMzBdOwoKJHJlc3VsdHMgPSBhcnJheV9tYXAoCglmbiAoJGl0ZW0pID0-ICRpdGVtICogMiwKCSRsaXN0Cik7CgpwcmludF9yKCRyZXN1bHRzKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; <span class=\"hljs-number\">20<\/span>\n    &#91;<span class=\"hljs-number\">1<\/span>] =&gt; <span class=\"hljs-number\">40<\/span>\n    &#91;<span class=\"hljs-number\">2<\/span>] =&gt; <span class=\"hljs-number\">60<\/span>\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>array_map()<\/code> function applies the arrow function to every element of the <code>$list<\/code> array and returns a new array that includes the results.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='returning-an-arrow-function-from-a-function'>Returning an arrow function from a function <a href=\"#returning-an-arrow-function-from-a-function\" class=\"anchor\" id=\"returning-an-arrow-function-from-a-function\" title=\"Anchor for Returning an arrow function from a function\">#<\/a><\/h2>\n\n\n\n<p>The following example illustrates how to return an arrow function from a function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">multiplier<\/span><span class=\"hljs-params\">($x)<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">return<\/span> fn ($y) =&gt; $x * $y;\n}\n\n$double = multiplier(<span class=\"hljs-number\">2<\/span>);\n\n<span class=\"hljs-keyword\">echo<\/span> $double(<span class=\"hljs-number\">10<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmZ1bmN0aW9uIG11bHRpcGxpZXIoJHgpCnsKCXJldHVybiBmbiAoJHkpID0-ICR4ICogJHk7Cn0KCiRkb3VibGUgPSBtdWx0aXBsaWVyKDIpOwoKZWNobyAkZG91YmxlKDEwKTsgLy8gMjAw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-number\">20<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define a function called <code>multiplier()<\/code> that accepts an argument and returns an arrow function. Since the arrow function can access the variable from its parent scope, we can use the <code>$x<\/code> parameter inside the arrow function.<\/li>\n\n\n\n<li>Second, call the <code>multiplier()<\/code> function and assign the returned value to the <code>$double<\/code> variable. The returned value of the <code>multiplier()<\/code> function is a function; therefore, we can call it via the <code>$double<\/code> variable.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An arrow function provides a shorter syntax for writing a short anonymous function.<\/li>\n\n\n\n<li>An arrow function starts with the fn keyword and contains only one expression, the function&#8217;s return value.<\/li>\n\n\n\n<li>An arrow function has access to the variables in its parent scope automatically.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"783\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-arrow-functions\/\"\n\t\t\t\tdata-post-title=\"PHP Arrow Functions\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"783\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-arrow-functions\/\"\n\t\t\t\tdata-post-title=\"PHP Arrow Functions\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn about PHP arrow functions and how to use them effectively. Introduction to PHP arrow functions # PHP 7.4 introduced arrow functions that provide a more concise syntax for the anonymous functions. The following illustrates the basic syntax for arrow functions: In this syntax, an arrow function: The arrow [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":67,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-783","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=783"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/783\/revisions"}],"predecessor-version":[{"id":3118,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/783\/revisions\/3118"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}