Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8889

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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>Syntax</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.constants.html">Constants</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.constants.predefined.html">Magic constants</a></div>
 <div class="up"><a href="language.constants.html">Constants</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.constants.syntax" class="sect1">
   <h2 class="title">Syntax</h2>
   <p class="simpara">
    You can define a constant by using the 
     <span class="function"><a href="function.define.html" class="function">define()</a></span>-function or by using the 
    <em>const</em> keyword outside a class definition as 
    of PHP 5.3.0. Once a constant is defined, it can never be 
    changed or undefined.
   </p>
   <p class="simpara">
    Only scalar data (<span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span>, <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>, 
    <span class="type"><a href="language.types.float.html" class="type float">float</a></span> and <span class="type"><a href="language.types.string.html" class="type string">string</a></span>) can be contained 
    in constants. It is possible to define constants as a 
    <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>, but it should be avoided, as it can cause 
    unexpected results.
   </p>
   <p class="simpara">
    You can get the value of a constant by simply specifying its name.
    Unlike with variables, you should <em class="emphasis">not</em> prepend
    a constant with a <em>$</em>.
    You can also use the function  <span class="function"><a href="function.constant.html" class="function">constant()</a></span> to
    read a constant&#039;s value if you wish to obtain the constant&#039;s name
    dynamically. 
    Use  <span class="function"><a href="function.get-defined-constants.html" class="function">get_defined_constants()</a></span> to get a list of 
    all defined constants.
   </p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     Constants and (global) variables are in a different namespace. 
     This implies that for example <strong><code>TRUE</code></strong> and 
     <var class="varname"><var class="varname">$TRUE</var></var> are generally different.
    </span>
   </p></blockquote>
   <p class="simpara">
    If you use an undefined constant, PHP assumes that you mean
    the name of the constant itself, just as if you called it as
    a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> (CONSTANT vs &quot;CONSTANT&quot;).  An error of level
    <a href="ref.errorfunc.html" class="link">E_NOTICE</a> will be issued
    when this happens.  See also the manual entry on why 
    <a href="language.types.array.html#language.types.array.foo-bar" class="link">$foo[bar]</a> is
    wrong (unless you first  <span class="function"><a href="function.define.html" class="function">define()</a></span>
    <em>bar</em> as a constant).  If you simply want to check if a
    constant is set, use the  <span class="function"><a href="function.defined.html" class="function">defined()</a></span> function.
   </p>
   <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 (<em>$</em>)
       before them;
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       Constants may only be defined using the
        <span class="function"><a href="function.define.html" class="function">define()</a></span> function, not by simple assignment;
      </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.
       </span>
     </li>
    </ul>
   </p>

   <p class="para">
    <div class="example" id="example-113">
     <p><strong>Example #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">,&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;world."</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">CONSTANT</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;outputs&nbsp;"Hello&nbsp;world."<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">Constant</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;outputs&nbsp;"Constant"&nbsp;and&nbsp;issues&nbsp;a&nbsp;notice.<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>

   <p class="para">
    <div class="example" id="example-114">
     <p><strong>Example #2 Defining Constants using the <em>const</em> 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">//&nbsp;Works&nbsp;as&nbsp;of&nbsp;PHP&nbsp;5.3.0<br /></span><span style="color: #007700">const&nbsp;</span><span style="color: #0000BB">CONSTANT&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Hello&nbsp;World'</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">CONSTANT</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     As opposed to defining constants using  <span class="function"><a href="function.define.html" class="function">define()</a></span>,
     constants defined using the <em>const</em> 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 or
     <em>if</em> statements.
    </p>
   </p></blockquote>

   <p class="simpara">
    See also <a href="language.oop5.constants.html" class="link">Class Constants</a>.
   </p>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.constants.html">Constants</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.constants.predefined.html">Magic constants</a></div>
 <div class="up"><a href="language.constants.html">Constants</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>