<HTML ><HEAD ><TITLE >Constants</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Language Reference" HREF="langref.html"><LINK REL="PREVIOUS" TITLE="Variables from outside PHP" HREF="language.variables.external.html"><LINK REL="NEXT" TITLE="Predefined constants" HREF="language.constants.predefined.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="chapter" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.variables.external.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.constants.predefined.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.constants" >Chapter 9. Constants</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="language.constants.html#language.constants.syntax" >Syntax</A ></DT ><DT ><A HREF="language.constants.predefined.html" >Predefined constants</A ></DT ></DL ></DIV ><P > A constant is a identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except the <A HREF="language.constants.predefined.html" > magic constants</A > which aren't actually constants). A constant is case-sensitive by default. By convention constant identifiers are always uppercase. </P ><P > 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 thus: <TT CLASS="literal" >[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</TT > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff). </P ></BLOCKQUOTE ></DIV ><P > The scope of a constant is global--you can access it anywhere in your script without regard to scope. </P ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.constants.syntax" ></A >Syntax</H1 ><P > You can define a constant by using the <A HREF="function.define.html" ><B CLASS="function" >define()</B ></A >-function. Once a constant is defined, it can never be changed or undefined. </P ><P > Only scalar data (<A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A >, <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >, <A HREF="language.types.float.html" ><B CLASS="type" >float</B ></A > and <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A >) can be contained in constants. </P ><P > You can get the value of a constant by simply specifying its name. Unlike with variables, you should <SPAN CLASS="emphasis" ><I CLASS="emphasis" >not</I ></SPAN > prepend a constant with a <TT CLASS="literal" >$</TT >. You can also use the function <A HREF="function.constant.html" ><B CLASS="function" >constant()</B ></A >, to read a constant's value, if you are to obtain the constant's name dynamically. Use <A HREF="function.get-defined-constants.html" ><B CLASS="function" >get_defined_constants()</B ></A > to get a list of all defined constants. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Constants and (global) variables are in a different namespace. This implies that for example <TT CLASS="constant" ><B >TRUE</B ></TT > and <TT CLASS="varname" >$TRUE</TT > are generally different. </P ></BLOCKQUOTE ></DIV ><P > If you use an undefined constant, PHP assumes that you mean the name of the constant itself. A <A HREF="ref.errorfunc.html" >notice</A > will be issued when this happens. Use the <A HREF="function.defined.html" ><B CLASS="function" >defined()</B ></A >-function if you want to know if a constant is set. </P ><P > These are the differences between constants and variables: <P ></P ><UL ><LI ><P > Constants do not have a dollar sign (<TT CLASS="literal" >$</TT >) before them; </P ></LI ><LI ><P > Constants may only be defined using the <A HREF="function.define.html" ><B CLASS="function" >define()</B ></A > function, not by simple assignment; </P ></LI ><LI ><P > Constants may be defined and accessed anywhere without regard to variable scoping rules; </P ></LI ><LI ><P > Constants may not be redefined or undefined once they have been set; and </P ></LI ><LI ><P > Constants may only evaluate to scalar values. </P ></LI ></UL > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4314" ></A ><P ><B >Example 9-1. Defining Constants</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php define("CONSTANT", "Hello world."); echo CONSTANT; // outputs "Hello world." echo Constant; // outputs "Constant" and issues a notice. ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.variables.external.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.constants.predefined.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Variables from outside PHP</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="langref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Predefined constants</TD ></TR ></TABLE ></DIV ></BODY ></HTML >