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

   <p class="para">
    Values are returned by using the optional return statement. Any
    type may be returned, including arrays and objects. This causes the
    function to end its execution immediately and pass control back to
    the line from which it was called. See <span class="function"><a href="function.return.php" class="function">return</a></span>
    for more information.
   </p>

   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <p class="para">
     If the <span class="function"><a href="function.return.php" class="function">return</a></span> is omitted the value <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> will be
     returned.
    </p>
   </p></blockquote>

   <div class="sect2">
    <h3 class="title">Use of return</h3>
    <p class="para">
     <div class="example" id="example-1">
      <p><strong>Приклад #1 Use of <span class="function"><a href="function.return.php" class="function">return</a></span></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">square</span><span style="color: #007700">(</span><span style="color: #0000BB">$num</span><span style="color: #007700">)<br />{<br />    return </span><span style="color: #0000BB">$num </span><span style="color: #007700">* </span><span style="color: #0000BB">$num</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #0000BB">square</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">);   </span><span style="color: #FF8000">// outputs '16'.<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>

    <p class="para">
     A function can not return multiple values, but similar results can be
     obtained by returning an array.
    </p>
    <p class="para">
     <div class="example" id="example-2">
      <p><strong>Приклад #2 Returning an array to get multiple 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">small_numbers</span><span style="color: #007700">()<br />{<br />    return [</span><span style="color: #0000BB">0</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 />}<br /></span><span style="color: #FF8000">// Array destructuring will collect each member of the array individually<br /></span><span style="color: #007700">[</span><span style="color: #0000BB">$zero</span><span style="color: #007700">, </span><span style="color: #0000BB">$one</span><span style="color: #007700">, </span><span style="color: #0000BB">$two</span><span style="color: #007700">] = </span><span style="color: #0000BB">small_numbers</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Prior to 7.1.0, the only equivalent alternative is using list() construct<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$zero</span><span style="color: #007700">, </span><span style="color: #0000BB">$one</span><span style="color: #007700">, </span><span style="color: #0000BB">$two</span><span style="color: #007700">) = </span><span style="color: #0000BB">small_numbers</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
    <p class="para">
     To return a reference from a function, use the reference operator &amp; in
     both the function declaration and when assigning the returned value to a
     variable:
    </p>
    <p class="para">
     <div class="example" id="example-3">
      <p><strong>Приклад #3 Returning a reference from a function</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 &amp;</span><span style="color: #0000BB">returns_reference</span><span style="color: #007700">()<br />{<br />    return </span><span style="color: #0000BB">$someref</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$newref </span><span style="color: #007700">=&amp; </span><span style="color: #0000BB">returns_reference</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
    <p class="simpara">
     For more information on references, please check out <a href="language.references.php" class="link">References Explained</a>.
    </p>
   </div>
  </div><?php manual_footer($setup); ?>