<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.functions.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'functions.arrow.php',
    1 => 'Arrow Functions',
    2 => 'Arrow Functions',
  ),
  'up' => 
  array (
    0 => 'language.functions.php',
    1 => 'Functions',
  ),
  'prev' => 
  array (
    0 => 'functions.anonymous.php',
    1 => 'Anonymous functions',
  ),
  'next' => 
  array (
    0 => 'functions.first_class_callable_syntax.php',
    1 => 'First class callable syntax',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/functions.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="functions.arrow" class="sect1">
   <h2 class="title">Arrow Functions</h2>

   <p class="simpara">
    Arrow functions were introduced in PHP 7.4 as a more concise syntax for 
    <a href="functions.anonymous.php" class="link">anonymous functions</a>.
   </p>
   <p class="simpara">
    Both anonymous functions and arrow functions are implemented using the 
    <a href="class.closure.php" class="link"><span class="classname"><a href="class.closure.php" class="classname">Closure</a></span></a> class.
   </p>

   <p class="simpara">
    Arrow functions have the basic form 
    <code class="code">fn (argument_list) =&gt; expr</code>.
   </p>

   <p class="simpara">
    Arrow functions support the same features as 
    <a href="functions.anonymous.php" class="link">anonymous functions</a>, 
    except that using variables from the parent scope is always automatic.
   </p>

   <p class="simpara">
    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 <var class="varname">$fn1</var> and 
    <var class="varname">$fn2</var> behave the same way. 
   </p>

   <p class="para">
    <div class="example" id="example-1">
     <p><strong>Приклад #1 Arrow functions capture variables by value automatically</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$y </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fn1 </span><span style="color: #007700">= fn(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">$x </span><span style="color: #007700">+ </span><span style="color: #0000BB">$y</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// equivalent to using $y by value:<br /></span><span style="color: #0000BB">$fn2 </span><span style="color: #007700">= function (</span><span style="color: #0000BB">$x</span><span style="color: #007700">) use (</span><span style="color: #0000BB">$y</span><span style="color: #007700">) {<br />    return </span><span style="color: #0000BB">$x </span><span style="color: #007700">+ </span><span style="color: #0000BB">$y</span><span style="color: #007700">;<br />};<br /><br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$fn1</span><span style="color: #007700">(</span><span style="color: #0000BB">3</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

     <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
4
</pre></div>
      </div>
    </div>
   </p>
   <p class="simpara">
    This also works if the arrow functions are nested: 
   </p>
   <p class="para">
    <div class="example" id="example-2">
     <p><strong>Приклад #2 Arrow functions capture variables by value automatically, even when nested</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$z </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$fn </span><span style="color: #007700">= fn(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) =&gt; fn(</span><span style="color: #0000BB">$y</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">$x </span><span style="color: #007700">* </span><span style="color: #0000BB">$y </span><span style="color: #007700">+ </span><span style="color: #0000BB">$z</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// Outputs 51<br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$fn</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">)(</span><span style="color: #0000BB">10</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
   <p class="simpara">
    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: 
   </p>
   <p class="para">
    <div class="example" id="example-3">
     <p><strong>Приклад #3 Examples of arrow functions</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">fn(array </span><span style="color: #0000BB">$x</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br />static fn(</span><span style="color: #0000BB">$x</span><span style="color: #007700">): </span><span style="color: #0000BB">int </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br />fn(</span><span style="color: #0000BB">$x </span><span style="color: #007700">= </span><span style="color: #0000BB">42</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br />fn(&amp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br />fn&amp;(</span><span style="color: #0000BB">$x</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br />fn(</span><span style="color: #0000BB">$x</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$rest</span><span style="color: #007700">) =&gt; </span><span style="color: #0000BB">$rest</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
   <p class="simpara">
    Arrow functions use by-value variable binding.
    This is roughly equivalent to performing a <code class="code">use($x)</code> for every 
    variable <var class="varname">$x</var> used inside the arrow function.
    A by-value binding means that it is not possible to modify any values 
    from the outer scope. 
    <a href="functions.anonymous.php" class="link">Anonymous functions</a> 
    can be used instead for by-ref bindings.
   </p>
   <p class="para">
    <div class="example" id="example-4">
     <p><strong>Приклад #4 Values from the outer scope cannot be modified by arrow functions</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$x </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$fn </span><span style="color: #007700">= fn() =&gt; </span><span style="color: #0000BB">$x</span><span style="color: #007700">++; </span><span style="color: #FF8000">// Has no effect<br /></span><span style="color: #0000BB">$fn</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">);  </span><span style="color: #FF8000">// Outputs 1<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
   
   <div class="sect2">
    <h3 class="title">Журнал змін</h3>
    <p class="para">
     <table class="doctable informaltable">
      
       <thead>
        <tr>
         <th>Версія</th>
         <th>Опис</th>
        </tr>

       </thead>

       <tbody class="tbody">
        <tr>
         <td>7.4.0</td>
         <td>
          Arrow functions became available.
         </td>
        </tr>

       </tbody>
      
     </table>

    </p>
   </div>

   <div class="sect2">
    <h3 class="title">Примітки</h3>
    <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
     <span class="simpara">
      It is possible to use <span class="function"><a href="function.func-num-args.php" class="function">func_num_args()</a></span>,
      <span class="function"><a href="function.func-get-arg.php" class="function">func_get_arg()</a></span>, and <span class="function"><a href="function.func-get-args.php" class="function">func_get_args()</a></span>
      from within an arrow function.
     </span>
    </p></blockquote>
   </div>
  </div><?php manual_footer($setup); ?>