<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.constants.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'language.constants.syntax.php',
    1 => 'Syntax',
    2 => 'Syntax',
  ),
  'up' => 
  array (
    0 => 'language.constants.php',
    1 => 'Constants',
  ),
  'prev' => 
  array (
    0 => 'language.constants.php',
    1 => 'Constants',
  ),
  'next' => 
  array (
    0 => 'language.constants.predefined.php',
    1 => 'Predefined constants',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/constants.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.constants.syntax" class="sect1">
   <h2 class="title">Syntax</h2>
   <p class="simpara">
    Constants can be defined using the <code class="literal">const</code> keyword,
    or by using the <span class="function"><a href="function.define.php" class="function">define()</a></span>-function.
    While <span class="function"><a href="function.define.php" class="function">define()</a></span> allows a constant to be
    defined to an arbitrary expression, the <code class="literal">const</code> keyword has
    restrictions as outlined in the next paragraph.
    Once a constant is defined, it can never be
    changed or undefined.
   </p>
   <p class="simpara">
    When using the <code class="literal">const</code> keyword,
    only scalar (<span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>, <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>,
    <span class="type"><a href="language.types.float.php" class="type float">float</a></span> and <span class="type"><a href="language.types.string.php" class="type string">string</a></span>) expressions and constant
    <span class="type"><a href="language.types.array.php" class="type array">array</a></span>s containing only scalar expressions are accepted.
    It is possible to define constants as a <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>,
    but it should be avoided, as it can cause unexpected results.
   </p>
   <p class="simpara">
    The value of a constant is accessed simply by specifying its name.
    Unlike variables, a constant is <em>not</em> prepended
    with a <code class="literal">$</code>.
    It is also possible to use the <span class="function"><a href="function.constant.php" class="function">constant()</a></span> function to
    read a constant&#039;s value if the constant&#039;s name is obtained dynamically. 
    Use <span class="function"><a href="function.get-defined-constants.php" class="function">get_defined_constants()</a></span> to get a list of 
    all defined constants.
   </p>

   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     Constants and (global) variables are in a different namespace. 
     This implies that for example <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> and 
     <var class="varname">$TRUE</var> are generally different.
    </span>
   </p></blockquote>

   <p class="simpara">
    If an undefined constant is used an <span class="classname"><a href="class.error.php" class="classname">Error</a></span> is thrown.
    Prior to PHP 8.0.0, undefined constants would be interpreted as a bare
    word <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, i.e. (CONSTANT vs &quot;CONSTANT&quot;). 
    This fallback is deprecated as of PHP 7.2.0, and an error of level
    <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is issued when it happens.
    Prior to PHP 7.2.0, an error of level
    <a href="ref.errorfunc.php" class="link">E_NOTICE</a> has been issued instead.
    See also the manual entry on why 
    <a href="language.types.array.php#language.types.array.foo-bar" class="link">$foo[bar]</a> is
    wrong (unless <code class="literal">bar</code> is a constant).
    This does not apply to <a href="language.namespaces.rules.php" class="link">(fully) qualified constants</a>,
    which will always raise a <span class="classname"><a href="class.error.php" class="classname">Error</a></span> if undefined.
   </p>

   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     To check if a constant is set, use the <span class="function"><a href="function.defined.php" class="function">defined()</a></span> function.
    </span>
   </p></blockquote>

   <p class="para">
    These are the differences between constants and variables:
    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       Constants do not have a dollar sign (<code class="literal">$</code>)
       before them;
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       Constants may be defined and accessed anywhere without regard
       to variable scoping rules;
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       Constants may not be redefined or undefined once they have been
       set; and
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       Constants may only evaluate to scalar values or arrays.
      </span>
     </li>
    </ul>
   </p>

   <p class="para">
    <div class="example" id="example-2">
     <p><strong>Приклад #1 Defining Constants</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />define</span><span style="color: #007700">(</span><span style="color: #DD0000">"CONSTANT"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Hello world."</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">CONSTANT</span><span style="color: #007700">; </span><span style="color: #FF8000">// outputs "Hello world."<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">Constant</span><span style="color: #007700">; </span><span style="color: #FF8000">// Emits an Error: Undefined constant "Constant"<br />               // Prior to PHP 8.0.0, outputs "Constant" and issues a warning.<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>Приклад #2 Defining Constants using the <code class="literal">const</code> keyword</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">// Simple scalar value<br /></span><span style="color: #007700">const </span><span style="color: #0000BB">CONSTANT </span><span style="color: #007700">= </span><span style="color: #DD0000">'Hello World'</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">CONSTANT</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Scalar expression<br /></span><span style="color: #007700">const </span><span style="color: #0000BB">ANOTHER_CONST </span><span style="color: #007700">= </span><span style="color: #0000BB">CONSTANT</span><span style="color: #007700">.</span><span style="color: #DD0000">'; Goodbye World'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">ANOTHER_CONST</span><span style="color: #007700">;<br /><br />const </span><span style="color: #0000BB">ANIMALS </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'dog'</span><span style="color: #007700">, </span><span style="color: #DD0000">'cat'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bird'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">ANIMALS</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]; </span><span style="color: #FF8000">// outputs "cat"<br /><br />// Constant arrays<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'ANIMALS'</span><span style="color: #007700">, array(<br />    </span><span style="color: #DD0000">'dog'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'cat'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'bird'<br /></span><span style="color: #007700">));<br />echo </span><span style="color: #0000BB">ANIMALS</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]; </span><span style="color: #FF8000">// outputs "cat"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>

   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <p class="para">
     As opposed to defining constants using <span class="function"><a href="function.define.php" class="function">define()</a></span>,
     constants defined using the <code class="literal">const</code> keyword must be
     declared at the top-level scope because they are defined at compile-time.
     This means that they cannot be declared inside functions, loops,
     <code class="literal">if</code> statements or
     <code class="literal">try</code>/<code class="literal">catch</code> blocks.
    </p>
   </p></blockquote>

   <div class="sect2">
    <h3 class="title">Прогляньте також</h3>
    <p class="para">
     <ul class="simplelist">
      <li><a href="language.oop5.constants.php" class="link">Class Constants</a></li>
     </ul>
    </p>
   </div>
  </div><?php manual_footer($setup); ?>