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

   <p class="para">
    A function is defined using the <code class="literal">function</code> keyword,
    a name, a list of parameters (which might be empty) seperated by commas
    (<code class="literal">,</code>) enclosed in parentheses, followed by the body of
    the function enclosed in curly braces, such as the following:
   </p>
   <div class="example" id="example-1">
    <p><strong>Приклад #1 Declaring a new function named <code class="literal">foo</code></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">$arg_1</span><span style="color: #007700">, </span><span style="color: #0000BB">$arg_2</span><span style="color: #007700">, </span><span style="color: #FF8000">/* ..., */ </span><span style="color: #0000BB">$arg_n</span><span style="color: #007700">)<br />{<br />    echo </span><span style="color: #DD0000">"Example function.\n"</span><span style="color: #007700">;<br />    return </span><span style="color: #0000BB">$retval</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <p class="para">
     As of PHP 8.0.0, the list of parameters may have a trailing comma:
     <div class="informalexample">
      <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">$arg_1</span><span style="color: #007700">, </span><span style="color: #0000BB">$arg_2</span><span style="color: #007700">,) { }<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
   </p></blockquote>
   
   <p class="simpara">
    Any valid PHP code may appear inside the body of a function, even other
    functions and <a href="language.oop5.basic.php#language.oop5.basic.class" class="link">class</a>
    definitions.
   </p>
   <p class="para">
    Function names follow the same rules as other labels in PHP. A
    valid function name starts with a letter or underscore, followed
    by any number of letters, numbers, or underscores. As a regular
    expression, it would be expressed thus:
    <code class="code">^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$</code>.
   </p>
   <div class="tip"><strong class="tip">Підказка</strong><p class="simpara">Див. <a href="userlandnaming.php" class="xref">Посібник з назв в користувацьому коді</a>.</p></div>
   <p class="simpara">
    Functions need not be defined before they are referenced,
    <em>except</em> when a function is conditionally defined as
    shown in the two examples below.
   </p>
   <p class="para">
    When a function is defined in a conditional manner such as the two
    examples shown. Its definition must be processed <em>prior</em>
    to being called.
   </p>
   <p class="para">
    <div class="example" id="example-2">
     <p><strong>Приклад #2 Conditional functions</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$makefoo </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* We can't call foo() from here <br />   since it doesn't exist yet,<br />   but we can call bar() */<br /><br /></span><span style="color: #0000BB">bar</span><span style="color: #007700">();<br /><br />if (</span><span style="color: #0000BB">$makefoo</span><span style="color: #007700">) {<br />  function </span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />  {<br />    echo </span><span style="color: #DD0000">"I don't exist until program execution reaches me.\n"</span><span style="color: #007700">;<br />  }<br />}<br /><br /></span><span style="color: #FF8000">/* Now we can safely call foo()<br />   since $makefoo evaluated to true */<br /><br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$makefoo</span><span style="color: #007700">) </span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /><br />function </span><span style="color: #0000BB">bar</span><span style="color: #007700">() <br />{<br />  echo </span><span style="color: #DD0000">"I exist immediately upon program start.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
   <p class="para">
    <div class="example" id="example-3">
     <p><strong>Приклад #3 Functions within 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">foo</span><span style="color: #007700">() <br />{<br />  function </span><span style="color: #0000BB">bar</span><span style="color: #007700">() <br />  {<br />    echo </span><span style="color: #DD0000">"I don't exist until foo() is called.\n"</span><span style="color: #007700">;<br />  }<br />}<br /><br /></span><span style="color: #FF8000">/* We can't call bar() yet<br />   since it doesn't exist. */<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/* Now we can call bar(),<br />   foo()'s processing has<br />   made it accessible. */<br /><br /></span><span style="color: #0000BB">bar</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
   <p class="para">
    All functions and classes in PHP have the global scope - they can be
    called outside a function even if they were defined inside and vice versa.
   </p>
   <p class="simpara">
    PHP does not support function overloading, nor is it possible to
    undefine or redefine previously-declared functions.
   </p>
   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     Function names are case-insensitive for the ASCII characters <code class="literal">A</code> to <code class="literal">Z</code>, though it is usually good form
     to call functions as they appear in their declaration.
    </span>
   </p></blockquote>   
   <p class="simpara">
    Both <a href="functions.arguments.php#functions.variable-arg-list" class="link">variable number of
    arguments</a> and <a href="functions.arguments.php#functions.arguments.default" class="link">default
    arguments</a> are supported in functions. See also the function
    references for
    <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> for more information.
   </p>
   
   <p class="para">
    It is possible to call recursive functions in PHP.
    <div class="example" id="example-4">
     <p><strong>Приклад #4 Recursive 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">recursion</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)<br />{<br />    if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">20</span><span style="color: #007700">) {<br />        echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">recursion</span><span style="color: #007700">(</span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />    }<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     Recursive function/method calls with over 100-200 recursion levels can
     smash the stack and cause a termination of the current script. Especially,
     infinite recursion is considered a programming error.
    </span>
   </p></blockquote>
   </p>

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