<!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>Constants</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.variables.external.html">Variables From External Sources</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.constants.syntax.html">Syntax</a></div> <div class="up"><a href="langref.html">Language Reference</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.constants" class="chapter"> <h1>Constants</h1> <h2>Table of Contents</h2><ul class="chunklist chunklist_chapter"><li><a href="language.constants.syntax.html">Syntax</a></li><li><a href="language.constants.predefined.html">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.html" 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: <em>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</em> </p> <div class="tip"><strong class="tip">Tip</strong><p class="simpara">See also the <a href="userlandnaming.html" class="xref">Userland Naming Guide</a>.</p></div> <p class="para"> <div class="example" id="example-112"> <p><strong>Example #1 Valid and invalid constant names</strong></p> <div class="example-contents"> <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> <blockquote class="note"><p><strong class="note">Note</strong>: <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.html" 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.html" class="link">variable scope</a>. </p> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.variables.external.html">Variables From External Sources</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.constants.syntax.html">Syntax</a></div> <div class="up"><a href="langref.html">Language Reference</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>