<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.misc.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.constant.php',
    1 => 'constant',
    2 => 'Returns the value of a constant',
  ),
  'up' => 
  array (
    0 => 'ref.misc.php',
    1 => 'Misc. Functions',
  ),
  'prev' => 
  array (
    0 => 'function.connection-status.php',
    1 => 'connection_status',
  ),
  'next' => 
  array (
    0 => 'function.define.php',
    1 => 'define',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/misc/functions/constant.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.constant" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">constant</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">constant</span> &mdash; <span class="dc-title">Returns the value of a constant</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.constant-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>constant</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

  <p class="simpara">
   Return the value of the constant indicated by
   <code class="parameter">name</code>.
  </p>
  <p class="simpara">
   <span class="function"><strong>constant()</strong></span> is useful if you need to retrieve
   the value of a constant, but do not know its name.  I.e. it is
   stored in a variable or returned by a function.
  </p>
  <p class="simpara">
   This function works also with <a href="language.oop5.constants.php" class="link">class constants</a> and <a href="language.types.enumerations.php" class="link">enum cases</a>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.constant-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">name</code></dt>
     <dd>
      <p class="para">
       The constant name.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.constant-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the value of the constant.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.constant-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
   If the constant is not defined, an <span class="classname"><a href="class.error.php" class="classname">Error</a></span> exception is thrown.
   Prior to PHP 8.0.0, an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> level error was generated in that case.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.constant-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       If the constant is not defined, <span class="function"><strong>constant()</strong></span> now throws an
       <span class="classname"><a href="class.error.php" class="classname">Error</a></span> exception; previously an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>
       was generated, and <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> was returned.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.constant-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Using <span class="function"><strong>constant()</strong></span> with Constants</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />define</span><span style="color: #007700">(</span><span style="color: #DD0000">"MAXSIZE"</span><span style="color: #007700">, </span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">MAXSIZE</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">"MAXSIZE"</span><span style="color: #007700">); </span><span style="color: #FF8000">// same thing as the previous line<br /><br /><br /></span><span style="color: #007700">interface </span><span style="color: #0000BB">bar </span><span style="color: #007700">{<br />    const </span><span style="color: #0000BB">test </span><span style="color: #007700">= </span><span style="color: #DD0000">'foobar!'</span><span style="color: #007700">;<br />}<br /><br />class </span><span style="color: #0000BB">foo </span><span style="color: #007700">{<br />    const </span><span style="color: #0000BB">test </span><span style="color: #007700">= </span><span style="color: #DD0000">'foobar!'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$const </span><span style="color: #007700">= </span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">'bar::'</span><span style="color: #007700">. </span><span style="color: #0000BB">$const</span><span style="color: #007700">)); </span><span style="color: #FF8000">// string(7) "foobar!"<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo::'</span><span style="color: #007700">. </span><span style="color: #0000BB">$const</span><span style="color: #007700">)); </span><span style="color: #FF8000">// string(7) "foobar!"<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-2">
    <p><strong>Example #2 Using <span class="function"><strong>constant()</strong></span> with Enum Cases (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 /><br /></span><span style="color: #007700">enum </span><span style="color: #0000BB">Suit<br /></span><span style="color: #007700">{<br />    case </span><span style="color: #0000BB">Hearts</span><span style="color: #007700">;<br />    case </span><span style="color: #0000BB">Diamonds</span><span style="color: #007700">;<br />    case </span><span style="color: #0000BB">Clubs</span><span style="color: #007700">;<br />    case </span><span style="color: #0000BB">Spades</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$case </span><span style="color: #007700">= </span><span style="color: #DD0000">'Hearts'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">'Suit::'</span><span style="color: #007700">. </span><span style="color: #0000BB">$case</span><span style="color: #007700">)); </span><span style="color: #FF8000">// enum(Suit::Hearts)<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

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


 <div class="refsect1 seealso" id="refsect1-function.constant-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.define.php" class="function" rel="rdfs-seeAlso">define()</a> - Defines a named constant</span></li>
    <li><span class="function"><a href="function.defined.php" class="function" rel="rdfs-seeAlso">defined()</a> - Checks whether a constant with the given name exists</span></li>
    <li><span class="function"><a href="function.get-defined-constants.php" class="function" rel="rdfs-seeAlso">get_defined_constants()</a> - Returns an associative array with the names of all the constants and their values</span></li>
    <li>The section on <a href="language.constants.php" class="link">Constants</a></li>
   </ul>
  </p>
 </div>


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