Source of: /manual/en/language.constants.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/langref.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'language.constants.php',
1 => 'Constants',
),
'up' =>
array (
0 => 'langref.php',
1 => 'Language Reference',
),
'prev' =>
array (
0 => 'language.variables.external.php',
1 => 'Variables From External Sources',
),
'next' =>
array (
0 => 'language.constants.syntax.php',
1 => 'Syntax',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div>
<h1>Constants</h1>
<h2>Table of Contents</h2><ul class="chunklist chunklist_chapter"><li><a href="language.constants.syntax.php">Syntax</a></li><li><a href="language.constants.predefined.php">Magic constants</a></li></ul>
<p class="simpara">
A constant is an identifier (name) for a simple value. As the name
suggests, that value cannot change during the execution of the
script (except for <a href="language.constants.predefined.php" class="link">
magic constants</a>, which aren't actually constants).
A constant is case-sensitive by default. By convention, constant
identifiers are always uppercase.
</p>
<p class="para">
The name of a constant follows the same rules as any label in PHP. A
valid constant name starts with a letter or underscore, followed
by any number of letters, numbers, or underscores. As a regular
expression, it would be expressed thusly:
<i>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</i>
</p>
<div class="tip"><b class="tip">Tip</b><p class="simpara">See also the
<a href="userlandnaming.php" class="xref">Userland Naming Guide</a>.</p></div>
<p class="para">
</p><div class="example">
<p><b>Example #1 Valid and invalid constant names</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// Valid constant names<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">"FOO"</span><span style="color: #007700">, </span><span style="color: #DD0000">"something"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">"FOO2"</span><span style="color: #007700">, </span><span style="color: #DD0000">"something else"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">"FOO_BAR"</span><span style="color: #007700">, </span><span style="color: #DD0000">"something more"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Invalid constant names<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">"2FOO"</span><span style="color: #007700">, </span><span style="color: #DD0000">"something"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// This is valid, but should be avoided:<br />// PHP may one day provide a magical constant<br />// that will break your script<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">"__FOO__"</span><span style="color: #007700">, </span><span style="color: #DD0000">"something"</span><span style="color: #007700">); <br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<blockquote><p><b class="note">Note</b>:
<span class="simpara">
For our purposes here, a letter is a-z, A-Z, and the ASCII
characters from 127 through 255 (0x7f-0xff).
</span>
</p></blockquote>
<p class="simpara">
Like <a href="language.variables.predefined.php" class="link">superglobals</a>, the scope of a constant is global. You
can access constants anywhere in your script without regard to scope.
For more information on scope, read the manual section on
<a href="language.variables.scope.php" class="link">variable scope</a>.
</p>
</div>
<?php manual_footer(); ?>