[ Team LiB ] Previous Section Next Section

Constants

Variables offer a flexible way of storing data because you can change their values and the type of data they store at any time. If, however, you want to work with a value that you do not want to alter throughout your script's execution, you can define a constant. You must use PHP's built-in function define() to create a constant. After you have done this, the constant cannot be changed. To use the define() function, you must place the name of the constant and the value you want to give it within the call's parentheses. These values must be separated by a comma, like so:


define ("CONSTANT_NAME", 42);

The value you want to set can only be a number or a string. By convention, the name of the constant should be in uppercase letters. Constants are accessed with the constant name only; no dollar symbol is required. Listing 4.4 defines and accesses a constant.

Listing 4.4 Defining a Constant
 1: <!DOCTYPE html PUBLIC
 2:   "-//W3C//DTD XHTML 1.0 Strict//EN"
 3:   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 4: <html>
 5: <head>
 6: <title>Listing 4.4 Defining a constant</title>
 7: </head>
 8: <body>
 9: <div>
10: <?php
11: define ("USER", "Gerald");
12: print "Welcome".USER;
13: ?>
14: </div>
15: </body>
16: </html>

Notice that in line 11 we used the concatenation operator to append the value held by our constant to the string "Welcome". This is because the PHP engine has no way of distinguishing between a constant and a string within quotation marks.

define() optionally accepts a third boolean argument that determines whether the constant name should be case insensitive. By default, constants are case sensitive, but by passing true to the define() function you can change this behavior. So, if we were to set up our USER constant in this way


Define ("USER", "Gerald", true);

we could access its value without worrying about case. So


print User;
print usEr;
print USER;

would all be equivalent. This feature can make scripts a little friendlier for programmers who work with your code, in that they will not need to consider case when accessing a constant you have defined. On the other hand, the fact that other constants are case sensitive could make for more rather than less confusion as programmers forget which constants to treat in which way. Unless you have a compelling reason to act otherwise, the safest course is to keep your constants case sensitive and define them using uppercase characters, which is an easily remembered convention.

Predefined Constants

PHP automatically provides some built-in constants for you. ___FILE___, for example, returns the name of the file currently being read by the PHP engine, and ___LINE___ returns the line number of the file. These constants are useful for generating error messages. You can also find out which version of PHP is interpreting the script with PHP_VERSION. This can be useful if you want to limit a script to run on a particular PHP release.

    [ Team LiB ] Previous Section Next Section