<?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.arguments.php',
    1 => 'Function parameters and arguments',
    2 => 'Function parameters and arguments',
  ),
  'up' => 
  array (
    0 => 'language.functions.php',
    1 => 'Functions',
  ),
  'prev' => 
  array (
    0 => 'functions.user-defined.php',
    1 => 'User-defined functions',
  ),
  'next' => 
  array (
    0 => 'functions.returning-values.php',
    1 => 'Returning values',
  ),
  '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.arguments" class="sect1">
   <h2 class="title">Function parameters and arguments</h2>

   <p class="simpara">
    The function parameters are declared in the function signature.
    Information may be passed to functions via the argument list,
    which is a comma-delimited list of expressions. The arguments are
    evaluated from left to right and the result is assigned to the parameters of
    the function, before the function is actually called
    (<em>eager</em> evaluation).
   </p>

   
   <p class="para">
    PHP supports passing arguments by value (the default), <a href="functions.arguments.php#functions.arguments.by-reference" class="link">passing by
    reference</a>, and <a href="functions.arguments.php#functions.arguments.default" class="link">default argument
    values</a>. <a href="functions.arguments.php#functions.variable-arg-list" class="link">Variable-length
    argument lists</a> and <a href="functions.arguments.php#functions.named-arguments" class="link">Named Arguments</a>
    are also supported.
   </p>
   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <p class="para">
     As of PHP 7.3.0, it is possible to have a trailing comma in the argument
     list for a function calls:
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$v </span><span style="color: #007700">= </span><span style="color: #0000BB">foo</span><span style="color: #007700">(<br />    </span><span style="color: #0000BB">$arg_1</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$arg_2</span><span style="color: #007700">,<br />);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
   </p></blockquote>

   <p class="para">
    As of PHP 8.0.0, the list of function parameters may include a trailing comma, which
    will be ignored. That is particularly useful in cases where the list of parameters is
    long or contains long variable names, making it convenient to list parameters vertically.
   </p>
   <div class="example" id="example-1">
    <p><strong>Приклад #1 Function parameter list with trailing comma</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">takes_many_args</span><span style="color: #007700">(<br />    </span><span style="color: #0000BB">$first_arg</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$second_arg</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$a_very_long_argument_name</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$arg_with_default </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">,<br />    </span><span style="color: #0000BB">$again </span><span style="color: #007700">= </span><span style="color: #DD0000">'a default string'</span><span style="color: #007700">, </span><span style="color: #FF8000">// This trailing comma was not permitted before 8.0.0.<br /></span><span style="color: #007700">)<br />{<br />    </span><span style="color: #FF8000">// ...<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <div class="sect2" id="functions.arguments.by-reference">
    <h3 class="title">Passing arguments by reference</h3>

    <p class="simpara">
     By default, function arguments are passed by value (so that if
     the value of the argument within the function is changed, it does
     not get changed outside of the function). To allow a function to modify its
     arguments, they must be passed by reference.
    </p>
    <p class="para">
     To have an argument to a function always passed by reference, prepend an
     ampersand (&amp;) to the parameter name in the function definition:
    </p>
    <p class="para">
     <div class="example" id="example-2">
      <p><strong>Приклад #2 Passing function arguments by reference</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">add_some_extra</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">)<br />{<br />    </span><span style="color: #0000BB">$string </span><span style="color: #007700">.= </span><span style="color: #DD0000">'and something extra.'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'This is a string, '</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">add_some_extra</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$str</span><span style="color: #007700">;    </span><span style="color: #FF8000">// outputs 'This is a string, and something extra.'<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
    <p class="para">
     It is an error to pass a constant expression as an argument to a parameter
     that expects to be passed by reference.
    </p>
   </div>
   <div class="sect2" id="functions.arguments.default">
    <h3 class="title">Default parameter values</h3>

    <p class="para">
     A function may define default values for parameters using syntax similar
     to assigning a variable. The default is used only when the parameter&#039;s argument is
     not passed. Note that passing <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> does <em>not</em>
     assign the default value.
    </p>
    <p class="para">
     <div class="example" id="example-3">
      <p><strong>Приклад #3 Use of default parameters in functions</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">makecoffee</span><span style="color: #007700">(</span><span style="color: #0000BB">$type </span><span style="color: #007700">= </span><span style="color: #DD0000">"cappuccino"</span><span style="color: #007700">)<br />{<br />    return </span><span style="color: #DD0000">"Making a cup of </span><span style="color: #0000BB">$type</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #0000BB">makecoffee</span><span style="color: #007700">();<br />echo </span><span style="color: #0000BB">makecoffee</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">makecoffee</span><span style="color: #007700">(</span><span style="color: #DD0000">"espresso"</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>
Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
</pre></div>
      </div>
     </div>
    </p>
    <p class="para">
     Default parameter values may be scalar values, <span class="type"><a href="language.types.array.php" class="type array">array</a></span>s,
     the special type <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>, and as of PHP 8.1.0, objects using the
     <a href="language.oop5.basic.php#language.oop5.basic.new" class="link">new ClassName()</a> syntax.
    </p>
    <p class="para">
     <div class="example" id="example-4">
      <p><strong>Приклад #4 Using non-scalar types as default values</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">makecoffee</span><span style="color: #007700">(</span><span style="color: #0000BB">$types </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"cappuccino"</span><span style="color: #007700">), </span><span style="color: #0000BB">$coffeeMaker </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">)<br />{<br />    </span><span style="color: #0000BB">$device </span><span style="color: #007700">= </span><span style="color: #0000BB">is_null</span><span style="color: #007700">(</span><span style="color: #0000BB">$coffeeMaker</span><span style="color: #007700">) ? </span><span style="color: #DD0000">"hands" </span><span style="color: #007700">: </span><span style="color: #0000BB">$coffeeMaker</span><span style="color: #007700">;<br />    return </span><span style="color: #DD0000">"Making a cup of "</span><span style="color: #007700">.</span><span style="color: #0000BB">join</span><span style="color: #007700">(</span><span style="color: #DD0000">", "</span><span style="color: #007700">, </span><span style="color: #0000BB">$types</span><span style="color: #007700">).</span><span style="color: #DD0000">" with </span><span style="color: #0000BB">$device</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #0000BB">makecoffee</span><span style="color: #007700">();<br />echo </span><span style="color: #0000BB">makecoffee</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"cappuccino"</span><span style="color: #007700">, </span><span style="color: #DD0000">"lavazza"</span><span style="color: #007700">), </span><span style="color: #DD0000">"teapot"</span><span style="color: #007700">);</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>
Making a cup of cappuccino with hands.
Making a cup of cappuccino, lavazza with teapot.
</pre></div>
      </div>
     </div>
    </p>
    <p class="para">
     <div class="example" id="example-5">
      <p><strong>Приклад #5 Using objects as default values (as of PHP 8.1.0)</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">DefaultCoffeeMaker </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">brew</span><span style="color: #007700">() {<br />        return </span><span style="color: #DD0000">"Making coffee.\n"</span><span style="color: #007700">;<br />    }<br />}<br />class </span><span style="color: #0000BB">FancyCoffeeMaker </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">brew</span><span style="color: #007700">() {<br />        return </span><span style="color: #DD0000">"Crafting a beautiful coffee just for you.\n"</span><span style="color: #007700">;<br />    }<br />}<br />function </span><span style="color: #0000BB">makecoffee</span><span style="color: #007700">(</span><span style="color: #0000BB">$coffeeMaker </span><span style="color: #007700">= new </span><span style="color: #0000BB">DefaultCoffeeMaker</span><span style="color: #007700">)<br />{<br />    return </span><span style="color: #0000BB">$coffeeMaker</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">brew</span><span style="color: #007700">();<br />}<br />echo </span><span style="color: #0000BB">makecoffee</span><span style="color: #007700">();<br />echo </span><span style="color: #0000BB">makecoffee</span><span style="color: #007700">(new </span><span style="color: #0000BB">FancyCoffeeMaker</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>
Making coffee.
Crafting a beautiful coffee just for you.
</pre></div>
      </div>
     </div>
    </p>
    <p class="simpara">
     The default value must be a constant expression, not (for
     example) a variable, a class member or a function call.
    </p>
    <p class="para">
     Note that any optional parameters should be specified after any
     required parameters, otherwise they cannot be omitted from calls.
     Consider the following example:
    </p>
    <p class="para">
     <div class="example" id="example-6">
      <p><strong>Приклад #6 Incorrect usage of default function parameters</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">makeyogurt</span><span style="color: #007700">(</span><span style="color: #0000BB">$container </span><span style="color: #007700">= </span><span style="color: #DD0000">"bowl"</span><span style="color: #007700">, </span><span style="color: #0000BB">$flavour</span><span style="color: #007700">)<br />{<br />    return </span><span style="color: #DD0000">"Making a </span><span style="color: #0000BB">$container</span><span style="color: #DD0000"> of </span><span style="color: #0000BB">$flavour</span><span style="color: #DD0000"> yogurt.\n"</span><span style="color: #007700">;<br />}<br /><br />echo </span><span style="color: #0000BB">makeyogurt</span><span style="color: #007700">(</span><span style="color: #DD0000">"raspberry"</span><span style="color: #007700">); </span><span style="color: #FF8000">// "raspberry" is $container, not $flavour<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>
Fatal error: Uncaught ArgumentCountError: Too few arguments
 to function makeyogurt(), 1 passed in example.php on line 42
</pre></div>
      </div>
     </div>
    </p>
    <p class="para">
     Now, compare the above with this:
    </p>
    <p class="para">
     <div class="example" id="example-7">
      <p><strong>Приклад #7 Correct usage of default function parameters</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">makeyogurt</span><span style="color: #007700">(</span><span style="color: #0000BB">$flavour</span><span style="color: #007700">, </span><span style="color: #0000BB">$container </span><span style="color: #007700">= </span><span style="color: #DD0000">"bowl"</span><span style="color: #007700">)<br />{<br />    return </span><span style="color: #DD0000">"Making a </span><span style="color: #0000BB">$container</span><span style="color: #DD0000"> of </span><span style="color: #0000BB">$flavour</span><span style="color: #DD0000"> yogurt.\n"</span><span style="color: #007700">;<br />}<br /><br />echo </span><span style="color: #0000BB">makeyogurt</span><span style="color: #007700">(</span><span style="color: #DD0000">"raspberry"</span><span style="color: #007700">); </span><span style="color: #FF8000">// "raspberry" is $flavour<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>
Making a bowl of raspberry yogurt.
</pre></div>
      </div>
     </div>
    </p>
    <p class="para">
     As of PHP 8.0.0, <a href="functions.arguments.php#functions.named-arguments" class="link">named arguments</a>
     can be used to skip over multiple optional parameters.
    </p>
    <p class="para">
     <div class="example" id="example-8">
      <p><strong>Приклад #8 Correct usage of default function parameters</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">makeyogurt</span><span style="color: #007700">(</span><span style="color: #0000BB">$container </span><span style="color: #007700">= </span><span style="color: #DD0000">"bowl"</span><span style="color: #007700">, </span><span style="color: #0000BB">$flavour </span><span style="color: #007700">= </span><span style="color: #DD0000">"raspberry"</span><span style="color: #007700">, </span><span style="color: #0000BB">$style </span><span style="color: #007700">= </span><span style="color: #DD0000">"Greek"</span><span style="color: #007700">)<br />{<br />    return </span><span style="color: #DD0000">"Making a </span><span style="color: #0000BB">$container</span><span style="color: #DD0000"> of </span><span style="color: #0000BB">$flavour</span><span style="color: #DD0000"> </span><span style="color: #0000BB">$style</span><span style="color: #DD0000"> yogurt.\n"</span><span style="color: #007700">;<br />}<br /><br />echo </span><span style="color: #0000BB">makeyogurt</span><span style="color: #007700">(</span><span style="color: #0000BB">style</span><span style="color: #007700">: </span><span style="color: #DD0000">"natural"</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>
Making a bowl of raspberry natural yogurt.
</pre></div>
      </div>
     </div>
    </p>
    <p class="para">
     As of PHP 8.0.0, declaring mandatory parameters after optional parameters
     is <em>deprecated</em>. This can generally be resolved by
     dropping the default value, since it will never be used.
     One exception to this rule are parameters of the form
     <code class="code">Type $param = null</code>, where the <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> default makes the type implicitly
     nullable. This usage is deprecated as of PHP 8.4.0, and an explicit
     <a href="language.types.declarations.php#language.types.declarations.nullable" class="link">nullable type</a>
     should be used instead.
     <div class="example" id="example-9">
      <p><strong>Приклад #9 Declaring optional parameters after mandatory parameters</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">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$a </span><span style="color: #007700">= [], </span><span style="color: #0000BB">$b</span><span style="color: #007700">) {}     </span><span style="color: #FF8000">// Default not used; deprecated as of PHP 8.0.0<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">) {}          </span><span style="color: #FF8000">// Functionally equivalent, no deprecation notice<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">bar</span><span style="color: #007700">(</span><span style="color: #0000BB">A $a </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">) {} </span><span style="color: #FF8000">// As of PHP 8.1.0, $a is implicitly required<br />                                 // (because it comes before the required one),<br />                                 // but implicitly nullable (deprecated as of PHP 8.4.0),<br />                                 // because the default parameter value is null<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">bar</span><span style="color: #007700">(?</span><span style="color: #0000BB">A $a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">) {}       </span><span style="color: #FF8000">// Recommended<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
    <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
     <span class="simpara">
      As of PHP 7.1.0, omitting a parameter which does not specify a default
      throws an <span class="classname"><a href="class.argumentcounterror.php" class="classname">ArgumentCountError</a></span>; in previous versions
      it raised a Warning.
     </span>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
     <span class="simpara">
      Parameters that expect the argument by reference may have a default value.
     </span>
    </p></blockquote>
   </div>

   <div class="sect2" id="functions.variable-arg-list">
    <h3 class="title">Variable-length argument lists</h3>

    <p class="simpara">
     PHP has support for variable-length argument lists in
     user-defined functions by using the
     <code class="literal">...</code> token.
    </p>

    <p class="para">
     Parameter lists may include the
     <code class="literal">...</code> token to denote that the function accepts a
     variable number of arguments. The arguments will be passed into the
     given variable as an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>:

     <div class="example" id="example-10">
      <p><strong>Приклад #10 Using <code class="literal">...</code> to access variable arguments</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">sum</span><span style="color: #007700">(...</span><span style="color: #0000BB">$numbers</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$acc </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />    foreach (</span><span style="color: #0000BB">$numbers </span><span style="color: #007700">as </span><span style="color: #0000BB">$n</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$acc </span><span style="color: #007700">+= </span><span style="color: #0000BB">$n</span><span style="color: #007700">;<br />    }<br />    return </span><span style="color: #0000BB">$acc</span><span style="color: #007700">;<br />}<br /><br />echo </span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</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>
10
</pre></div>
      </div>
     </div>
    </p>

    <p class="para">
     <code class="literal">...</code> can also be used when calling functions to unpack
     an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> or <span class="classname"><a href="class.traversable.php" class="classname">Traversable</a></span> variable or
     literal into the argument list:

     <div class="example" id="example-11">
      <p><strong>Приклад #11 Using <code class="literal">...</code> to provide arguments</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">) {<br />    return </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}<br /><br />echo </span><span style="color: #0000BB">add</span><span style="color: #007700">(...[</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">]).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">];<br />echo </span><span style="color: #0000BB">add</span><span style="color: #007700">(...</span><span style="color: #0000BB">$a</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>
3
3
</pre></div>
      </div>
     </div>
    </p>

    <p class="para">
     You may specify normal positional parameters before the
     <code class="literal">...</code> token. In this case, only the trailing arguments
     that don&#039;t match a positional argument will be added to the array
     generated by <code class="literal">...</code>.
    </p>

    <p class="para">
     It is also possible to add a
     <a href="language.types.declarations.php" class="link">type declaration</a> before the
     <code class="literal">...</code> token. If this is present, then all arguments
     captured by <code class="literal">...</code> must match that parameter type.

     <div class="example" id="example-12">
      <p><strong>Приклад #12 Type declared variable arguments</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">total_intervals</span><span style="color: #007700">(</span><span style="color: #0000BB">$unit</span><span style="color: #007700">, </span><span style="color: #0000BB">DateInterval </span><span style="color: #007700">...</span><span style="color: #0000BB">$intervals</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$time </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />    foreach (</span><span style="color: #0000BB">$intervals </span><span style="color: #007700">as </span><span style="color: #0000BB">$interval</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$time </span><span style="color: #007700">+= </span><span style="color: #0000BB">$interval</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$unit</span><span style="color: #007700">;<br />    }<br />    return </span><span style="color: #0000BB">$time</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P1D'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P2D'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">total_intervals</span><span style="color: #007700">(</span><span style="color: #DD0000">'d'</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">).</span><span style="color: #DD0000">' days'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// This will fail, since null isn't a DateInterval object.<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">total_intervals</span><span style="color: #007700">(</span><span style="color: #DD0000">'d'</span><span style="color: #007700">, </span><span style="color: #0000BB">null</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>
3 days
Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2
</pre></div>
      </div>
     </div>
    </p>

    <p class="para">
     Finally, variable arguments can also be passed
     <a href="functions.arguments.php#functions.arguments.by-reference" class="link">by reference</a> by
     prefixing the <code class="literal">...</code> with an ampersand
     (<code class="literal">&amp;</code>).
    </p>

   </div>

   <div class="sect2" id="functions.named-arguments">
    <h3 class="title">Named Arguments</h3>

    <p class="para">
     PHP 8.0.0 introduced named arguments as an extension of the existing
     positional parameters. Named arguments allow passing arguments to a
     function based on the parameter name, rather than the parameter position.
     This makes the meaning of the argument self-documenting, makes the
     arguments order-independent and allows skipping default values arbitrarily.
    </p>

    <p class="para">
     Named arguments are passed by prefixing the value with the parameter name
     followed by a colon. Using reserved keywords as parameter names is allowed.
     The parameter name must be an identifier, specifying dynamically
     is not allowed.
    </p>

    <div class="example" id="example-13">
     <p><strong>Приклад #13 Named argument syntax</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />myFunction</span><span style="color: #007700">(</span><span style="color: #0000BB">paramName</span><span style="color: #007700">: </span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_foobar</span><span style="color: #007700">(array: </span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// NOT supported.<br /></span><span style="color: #0000BB">function_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$variableStoringParamName</span><span style="color: #007700">: </span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>

    <div class="example" id="example-14">
     <p><strong>Приклад #14 Positional arguments versus named arguments</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Using positional arguments:<br /></span><span style="color: #0000BB">array_fill</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">100</span><span style="color: #007700">, </span><span style="color: #0000BB">50</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Using named arguments:<br /></span><span style="color: #0000BB">array_fill</span><span style="color: #007700">(</span><span style="color: #0000BB">start_index</span><span style="color: #007700">: </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">count</span><span style="color: #007700">: </span><span style="color: #0000BB">100</span><span style="color: #007700">, </span><span style="color: #0000BB">value</span><span style="color: #007700">: </span><span style="color: #0000BB">50</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>

    <p class="para">
     The order in which the named arguments are passed does not matter.
    </p>

    <div class="example" id="example-15">
     <p><strong>Приклад #15 Same example as above with a different order of parameters</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />array_fill</span><span style="color: #007700">(</span><span style="color: #0000BB">value</span><span style="color: #007700">: </span><span style="color: #0000BB">50</span><span style="color: #007700">, </span><span style="color: #0000BB">count</span><span style="color: #007700">: </span><span style="color: #0000BB">100</span><span style="color: #007700">, </span><span style="color: #0000BB">start_index</span><span style="color: #007700">: </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>

    <p class="para">
     Named arguments can be combined with positional arguments. In this case,
     the named arguments must come after the positional arguments.
     It is also possible to specify only some of the optional arguments of a
     function, regardless of their order.
    </p>

    <div class="example" id="example-16">
     <p><strong>Приклад #16 Combining named arguments with positional arguments</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #0000BB">double_encode</span><span style="color: #007700">: </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Same as<br /></span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #0000BB">ENT_QUOTES </span><span style="color: #007700">| </span><span style="color: #0000BB">ENT_SUBSTITUTE </span><span style="color: #007700">| </span><span style="color: #0000BB">ENT_HTML401</span><span style="color: #007700">, </span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>

    <p class="para">
     Passing an argument to the same named parameter multiple times results in an
     <span class="classname"><a href="class.error.php" class="classname">Error</a></span> exception.
    </p>

    <div class="example" id="example-17">
     <p><strong>Приклад #17 Error thrown when passing an argument to the same named parameter multiple times</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">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$param</span><span style="color: #007700">) { ... }<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">param</span><span style="color: #007700">: </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">param</span><span style="color: #007700">: </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Error: Named parameter $param overwrites previous argument<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">param</span><span style="color: #007700">: </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Error: Named parameter $param overwrites previous argument<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>

    <p class="para">
     As of PHP 8.1.0, it is possible to use named arguments after unpacking the arguments.
     A named argument <em>must not</em> override an already unpacked argument.
    </p>

    <div class="example" id="example-18">
     <p><strong>Приклад #18 Use named arguments after unpacking</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">$d </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">) {<br />  return </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b </span><span style="color: #007700">+ </span><span style="color: #0000BB">$c </span><span style="color: #007700">+ </span><span style="color: #0000BB">$d</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">foo</span><span style="color: #007700">(...[</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">], </span><span style="color: #0000BB">d</span><span style="color: #007700">: </span><span style="color: #0000BB">40</span><span style="color: #007700">)); </span><span style="color: #FF8000">// 46<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">foo</span><span style="color: #007700">(...[</span><span style="color: #DD0000">'b' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">'a' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">], </span><span style="color: #0000BB">d</span><span style="color: #007700">: </span><span style="color: #0000BB">40</span><span style="color: #007700">)); </span><span style="color: #FF8000">// 46<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">foo</span><span style="color: #007700">(...[</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">], </span><span style="color: #0000BB">b</span><span style="color: #007700">: </span><span style="color: #0000BB">20</span><span style="color: #007700">)); </span><span style="color: #FF8000">// Fatal error. Named parameter $b overwrites previous argument<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>

   </div>
  </div><?php manual_footer($setup); ?>