downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/language.constants.syntax.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = 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 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'language.constants.syntax.php',
   
1 => '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 => 'Magic constants',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="language.constants.syntax" class="sect1">
   <h2 class="title">Syntax</h2>
   <p class="simpara">
    You can define a constant by using the
    <a href="function.define.php" class="function">define()</a>-function or by using the
    <i>const</i> 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 (<a href="language.types.boolean.php" class="type boolean">boolean</a>, <a href="language.types.integer.php" class="type integer">integer</a>,
    <a href="language.types.float.php" class="type float">float</a> and <a href="language.types.string.php" class="type string">string</a>) can be contained
    in constants. It is possible to define constants as a
    <a href="language.types.resource.php" class="type resource">resource</a>, 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 <i>$</i>.
    You can also use the function <a href="function.constant.php" class="function">constant()</a> to
    read a constant&#039;s value if you wish to obtain the constant&#039;s name
    dynamically.
    Use <a href="function.get-defined-constants.php" class="function">get_defined_constants()</a> to get a list of
    all defined constants.
   </p>
   <blockquote><p><b class="note">Note</b>:
    <span class="simpara">
     Constants and (global) variables are in a different namespace.
     This implies that for example <b><tt class="constant">TRUE</tt></b> and
     <var class="varname">$TRUE</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 <a href="language.types.string.php" class="type string">string</a> (CONSTANT vs &quot;CONSTANT&quot;).  An error of level
    <a href="ref.errorfunc.php" class="link">E_NOTICE</a> will be issued
    when this happens.  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 you first <a href="function.define.php" class="function">define()</a>
    <i>bar</i> as a constant).  If you simply want to check if a
    constant is set, use the <a href="function.defined.php" class="function">defined()</a> function.
   </p>
   <p class="para">
    These are the differences between constants and variables:
    </p><ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       Constants do not have a dollar sign (<i>$</i>)
       before them;
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       Constants may only be defined using the
       <a href="function.define.php" class="function">define()</a> 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>

   <p class="para">
    </p><div class="example">
     <p><b>Example #1 Defining Constants</b></p>
     <div class="example-contents programlisting">
<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>

   <p class="para">
    </p><div class="example">
     <p><b>Example #2 Defining Constants using the <i>const</i> keyword</b></p>
     <div class="example-contents programlisting">
<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>
   </p>

   <p class="simpara">
    See also <a href="language.oop5.constants.php" class="link">Class Constants</a>.
   </p>
  </div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites