<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Returns the value of a constant</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.connection-status.html">connection_status</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.define.html">define</a></div> <div class="up"><a href="ref.misc.html">Misc. Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.constant" class="refentry"> <div class="refnamediv"> <h1 class="refname">constant</h1> <p class="verinfo">(PHP 4 >= 4.0.4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">constant</span> — <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="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>constant</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></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.html" class="link">class constants</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, or <strong><code>NULL</code></strong> if the constant is not defined. </p> </div> <div class="refsect1 errors" id="refsect1-function.constant-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> An <strong><code>E_WARNING</code></strong> level error is generated if the constant is not defined. </p> </div> <div class="refsect1 examples" id="refsect1-function.constant-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4660"> <p><strong>Example #1 <span class="function"><strong>constant()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?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">?></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 class="member"><span class="function"><a href="function.define.html" class="function" rel="rdfs-seeAlso">define()</a> - Defines a named constant</span></li> <li class="member"><span class="function"><a href="function.defined.html" class="function" rel="rdfs-seeAlso">defined()</a> - Checks whether a given named constant exists</span></li> <li class="member">The section on <a href="language.constants.html" class="link">Constants</a></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.connection-status.html">connection_status</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.define.html">define</a></div> <div class="up"><a href="ref.misc.html">Misc. Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>