{"id":776,"date":"2021-03-27T10:20:13","date_gmt":"2021-03-27T10:20:13","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=776"},"modified":"2025-04-07T13:45:57","modified_gmt":"2025-04-07T13:45:57","slug":"php-anonymous-functions","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-anonymous-functions\/","title":{"rendered":"PHP Anonymous Functions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about PHP anonymous functions and how to use them effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-anonymous-functions'>Introduction to anonymous functions <a href=\"#introduction-to-anonymous-functions\" class=\"anchor\" id=\"introduction-to-anonymous-functions\" title=\"Anchor for Introduction to anonymous functions\">#<\/a><\/h2>\n\n\n\n<p>When you <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-functions\/\">define a function<\/a>, you specify a name for it. Later, you can call the function by its name.<\/p>\n\n\n\n<p>For example, to define a function that multiplies two numbers, you can do it as follows:<\/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\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">multiply<\/span><span class=\"hljs-params\">($x, $y)<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">return<\/span> $x * $y;\n}<\/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>The <code>multiply()<\/code> function accepts two arguments and returns the result. To call the <code>multiply()<\/code> function, you pass the arguments to it like this:<\/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-meta\">&lt;?php<\/span>\n\nmultiply(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>);<\/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>In this example, the <code>multiply()<\/code> is a named function. And you can reuse it as many times as you want.<\/p>\n\n\n\n<p>Besides named functions, PHP allows you to define anonymous functions.<\/p>\n\n\n\n<p>An anonymous function is a function that doesn&#8217;t have a name.<\/p>\n\n\n\n<p>The following example defines an anonymous function that multiplies two numbers:<\/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<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($x, $y)<\/span> <\/span>{\n\t<span class=\"hljs-keyword\">return<\/span> $x * $y;\n};<\/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>Since the function doesn&#8217;t have a name, you need to end it with a semicolon (<code>;<\/code>) because PHP treats it as an expression.<\/p>\n\n\n\n<p>This anonymous function is useless because you cannot use it like a named function.<\/p>\n\n\n\n<p>To use an anonymous function, you need to assign it to a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">variable<\/a> and call the function via the variable.<\/p>\n\n\n\n<p>The following example assigns the anonymous function to the <code>$multiply<\/code> variable:<\/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$multiply = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($x, $y)<\/span> <\/span>{\n\t<span class=\"hljs-keyword\">return<\/span> $x * $y;\n};<\/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>And this calls the anonymous function via the <code>$multiply<\/code> variable:<\/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-meta\">&lt;?php<\/span>\n\n$multiply = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($x, $y)<\/span> <\/span>{\n\t<span class=\"hljs-keyword\">return<\/span> $x * $y;\n};\n\n<span class=\"hljs-keyword\">echo<\/span> $multiply(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>);<\/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><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRtdWx0aXBseSA9IGZ1bmN0aW9uICgkeCwgJHkpIHsKCXJldHVybiAkeCAqICR5Owp9OwplY2hvICRtdWx0aXBseSgxMCwgMjApOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>When you dump the information on the <code>$multiply<\/code> variable, you&#8217;ll see that it&#8217;s actually a <code>Clousure<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$multiply = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($x, $y)<\/span> <\/span>{\n\t<span class=\"hljs-keyword\">return<\/span> $x * $y;\n};\n\nvar_dump($multiply);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRtdWx0aXBseSA9IGZ1bmN0aW9uICgkeCwgJHkpIHsKCXJldHVybiAkeCAqICR5Owp9OwoKdmFyX2R1bXAoJG11bHRpcGx5KTs\" 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\">object(Closure)<span class=\"hljs-comment\">#1 (1) {<\/span>\n  &#91;<span class=\"hljs-string\">\"parameter\"<\/span>]=&gt; <span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-number\">2<\/span>) {\n    &#91;<span class=\"hljs-string\">\"$x\"<\/span>]=&gt;string(<span class=\"hljs-number\">10<\/span>) <span class=\"hljs-string\">\"&lt;required&gt;\"<\/span>\n    &#91;<span class=\"hljs-string\">\"$y\"<\/span>]=&gt;string(<span class=\"hljs-number\">10<\/span>) <span class=\"hljs-string\">\"&lt;required&gt;\"<\/span>\n  }\n}<\/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 class=\"note\">Note that the <code>Closure<\/code> in PHP is not the same as the closure in other programming languages such as <a href=\"https:\/\/www.javascripttutorial.net\/javascript-closure\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript <\/a>or <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-closures\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a>.<\/p>\n\n\n\n<p>Since an anonymous function is an object, you can <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">assign it to a variable<\/a>, pass it to a function, and return it from a function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='passing-an-anonymous-function-to-another-function'>Passing an anonymous function to another function <a href=\"#passing-an-anonymous-function-to-another-function\" class=\"anchor\" id=\"passing-an-anonymous-function-to-another-function\" title=\"Anchor for Passing an anonymous function to another function\">#<\/a><\/h2>\n\n\n\n<p>PHP has many built-in functions that accept a callback function, for example, the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array_map\/\">array_map()<\/a><\/code> function.<\/p>\n\n\n\n<p>The <code>array_map()<\/code> function accepts a callback function and an array. It applies the callback function to each element and includes the results in a new array.<\/p>\n\n\n\n<p>The following example shows how to double each number in an array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" 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\">double_it<\/span><span class=\"hljs-params\">($element)<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">return<\/span> $element * <span class=\"hljs-number\">2<\/span>;\n}\n\n$list = &#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>];\n$double_list = array_map(<span class=\"hljs-string\">'double_it'<\/span>, $list);\n\nprint_r($double_list);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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=PD9waHAgCgpmdW5jdGlvbiBkb3VibGVfaXQoJGVsZW1lbnQpCnsKCXJldHVybiAkZWxlbWVudCAqIDI7Cn0KCiRsaXN0ID0gWzEwLCAyMCwgMzBdOwokZG91YmxlX2xpc3QgPSBhcnJheV9tYXAoJ2RvdWJsZV9pdCcsICRsaXN0KTsKCnByaW50X3IoJGRvdWJsZV9saXN0KTs\" 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 a named function called <code>double_it<\/code> to double a number.<\/li>\n\n\n\n<li>Second, define an array of integers.<\/li>\n\n\n\n<li>Third, call the <code>array_map()<\/code> function to double each element of the <code>$list<\/code> array.<\/li>\n\n\n\n<li>Finally, show the result array.<\/li>\n<\/ul>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" 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-9\"><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>This example works perfectly fine. However, it&#8217;s quite verbose. And the <code>double_it<\/code> function may be used once.<\/p>\n\n\n\n<p>The following example does the same but uses an anonymous function instead:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" 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(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($element)<\/span> <\/span>{\n\t<span class=\"hljs-keyword\">return<\/span> $element * <span class=\"hljs-number\">2<\/span>;\n}, $list);\n\nprint_r($results);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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=PD9waHAgCgokbGlzdCA9IFsxMCwgMjAsIDMwXTsKCiRyZXN1bHRzID0gYXJyYXlfbWFwKGZ1bmN0aW9uICgkZWxlbWVudCkgewoJcmV0dXJuICRlbGVtZW50ICogMjsKfSwgJGxpc3QpOwoKcHJpbnRfcigkcmVzdWx0cyk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='scope-of-the-anonymous-function'>Scope of the anonymous function <a href=\"#scope-of-the-anonymous-function\" class=\"anchor\" id=\"scope-of-the-anonymous-function\" title=\"Anchor for Scope of the anonymous function\">#<\/a><\/h2>\n\n\n\n<p>By default, an anonymous function cannot access the <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">variables<\/a> from its parent <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variable-scopes\/\">scope<\/a>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" 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$message = <span class=\"hljs-string\">'Hi'<\/span>;\n$say = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">()<\/span> <\/span>{\n\t<span class=\"hljs-keyword\">echo<\/span> $message;\n};\n\n$say();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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>PHP issued the following notice:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">PHP Notice:  Undefined variable: message in ...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the anonymous function attempts to access the <code>$message<\/code> variable from its parent scope. However, it could not. Therefore, PHP issued a notice.<\/p>\n\n\n\n<p>To use the variables from the parent scope inside an anonymous function, you place the variables in the <code>use<\/code> construct as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" 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$message = <span class=\"hljs-string\">'Hi'<\/span>;\n$say = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">()<\/span> <span class=\"hljs-title\">use<\/span> <span class=\"hljs-params\">($message)<\/span> <\/span>{\n\t<span class=\"hljs-keyword\">echo<\/span> $message;\n};\n\n$say();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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=PD9waHAKCiRtZXNzYWdlID0gJ0hpJzsKJHNheSA9IGZ1bmN0aW9uICgpIHVzZSAoJG1lc3NhZ2UpIHsKCWVjaG8gJG1lc3NhZ2U7Cn07Cgokc2F5KCk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Hi<\/code><\/span><\/pre>\n\n\n<p>Now, it should work correctly.<\/p>\n\n\n\n<p>Note that the <code>$message<\/code> is passed to the anonymous function by value, not by reference. If you change it inside the anonymous function, the change will not reflect outside of the function. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" 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$message = <span class=\"hljs-string\">'Hi'<\/span>;\n$say = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">()<\/span> <span class=\"hljs-title\">use<\/span> <span class=\"hljs-params\">($message)<\/span> <\/span>{\n\t$message = <span class=\"hljs-string\">'Hello'<\/span>;\n\t<span class=\"hljs-keyword\">echo<\/span> $message . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;;\n};\n\n$say();\n\n<span class=\"hljs-keyword\">echo<\/span> $message . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><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=PD9waHAKCiRtZXNzYWdlID0gJ0hpJzsKJHNheSA9IGZ1bmN0aW9uICgpIHVzZSAoJG1lc3NhZ2UpIHsKCSRtZXNzYWdlID0gJ0hlbGxvJzsKCWVjaG8gJG1lc3NhZ2U7Cn07Cgokc2F5KCk7CgplY2hvICRtZXNzYWdlOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Hello\nHi<\/code><\/span><\/pre>\n\n\n<p>In this example, inside the anonymous function the value of the <code>$message<\/code> is <code>'Hello'<\/code>. However, outside of the anonymous function, the value of the message remains the same as <code>'Hi'<\/code>.<\/p>\n\n\n\n<p>If you want to pass a variable to an anonymous function by reference, you need to use the <code>&amp;<\/code> operator like the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" 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$message = <span class=\"hljs-string\">'Hi'<\/span>;\n$say = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">()<\/span> <span class=\"hljs-title\">use<\/span> <span class=\"hljs-params\">(&amp;$message)<\/span> <\/span>{\n\t$message = <span class=\"hljs-string\">'Hello'<\/span>;\n\t<span class=\"hljs-keyword\">echo<\/span> $message . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;;\n};\n\n$say();\n\n<span class=\"hljs-keyword\">echo<\/span> $message . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><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=PD9waHAKCiRtZXNzYWdlID0gJ0hpJzsKJHNheSA9IGZ1bmN0aW9uICgpIHVzZSAoJG1lc3NhZ2UpIHsKCSRtZXNzYWdlID0gJ0hlbGxvJzsKCWVjaG8gJG1lc3NhZ2U7Cn07Cgokc2F5KCk7CmVjaG8gJG1lc3NhZ2U7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Hello\nHello<\/code><\/span><\/pre>\n\n\n<p>Now, you see the <code>'Hello'<\/code> messages twice.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='return-an-anonymous-function-from-a-function'>Return an anonymous function from a function <a href=\"#return-an-anonymous-function-from-a-function\" class=\"anchor\" id=\"return-an-anonymous-function-from-a-function\" title=\"Anchor for Return an anonymous function from a function\">#<\/a><\/h2>\n\n\n\n<p>The following example illustrates how to return an anonymous function from a function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" 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> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($y)<\/span> <span class=\"hljs-title\">use<\/span> <span class=\"hljs-params\">($x)<\/span> <\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> $x * $y;\n\t};\n}\n\n$double = multiplier(<span class=\"hljs-number\">2<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $double(<span class=\"hljs-number\">100<\/span>) . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>; <span class=\"hljs-comment\">\/\/ 200<\/span>\n\n$tripple = multiplier(<span class=\"hljs-number\">3<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $tripple(<span class=\"hljs-number\">100<\/span>) . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>; <span class=\"hljs-comment\">\/\/ 300<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><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=PD9waHAKCmZ1bmN0aW9uIG11bHRpcGxpZXIoJHgpCnsKCXJldHVybiBmdW5jdGlvbiAoJHkpIHVzZSAoJHgpIHsKCQlyZXR1cm4gJHggKiAkeTsKCX07Cn0KCiRkb3VibGUgPSBtdWx0aXBsaWVyKDIpOwplY2hvICRkb3VibGUoMTAwKTsgLy8gMjAwCgokdHJpcHBsZSA9IG11bHRpcGxpZXIoMyk7CmVjaG8gJHRyaXBwbGUoMTAwKTsgLy8gMzAw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">200\n300<\/code><\/span><\/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>mutiplier<\/code> that returns an anonymous function.<\/li>\n\n\n\n<li>Second, call the <code>multiplier<\/code> function and assign its returned value to the <code>$double<\/code> variable. Since the return value is a function, it can be invoked like a regular function (<code>$double(2)<\/code>).<\/li>\n\n\n\n<li>Third, call the multiplier function and assign its returned value to the <code>$tripple<\/code> variable. This time we passed 3 instead of 2.<\/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 anonymous function is a function without a name.<\/li>\n\n\n\n<li>An anonymous function is a Closure object.<\/li>\n\n\n\n<li>To access the variables from the parent scope inside an anonymous function, place the variables in the <code>use<\/code> construct.<\/li>\n\n\n\n<li>An anonymous function can be assigned to a variable, passed to a function, or returned from a function.<\/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=\"776\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-anonymous-functions\/\"\n\t\t\t\tdata-post-title=\"PHP Anonymous 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=\"776\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-anonymous-functions\/\"\n\t\t\t\tdata-post-title=\"PHP Anonymous 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 anonymous functions and how to use them effectively. Introduction to anonymous functions # When you define a function, you specify a name for it. Later, you can call the function by its name. For example, to define a function that multiplies two numbers, you can do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":66,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-776","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/776","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=776"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/776\/revisions"}],"predecessor-version":[{"id":3276,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/776\/revisions\/3276"}],"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=776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}