Variables offer a flexible way of storing data. 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 define() function 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:

define( "CONSTANT_NAME", 42);

The value you want to set can be a number, a string, or a Boolean. By convention, the name of the constant should be in capital 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: <html>
  2: <head>
  3: <title>Listing 4.4 Defining a constant</title>
  4: </head>
  5: <body>
  6: <?php
  7: define( "USER", "Gerald");
  8: print "Welcome ".USER;
  9: ?>
 10: </body>
 11: </html>

Notice that in line 8 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.

Put these lines into a text file called constants.php, and place this file in your Web server document root. When you access this script through your Web browser, it produces the following:

Welcome Gerald

The define() function can accept a third Boolean argument that determines whether or not the constant name should be case-independent. By default, constants are case-dependent. However, by passing true to the define() function, we can change this behavior, so if we were to set up our USER constant as

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

we could access its value without worrying about case:

print User;
print usEr;
print USER;

These expressions are all equivalent. This feature can make scripts a little friendlier for programmers who work with our code, in that they will not need to consider case when accessing a constant that we have defined. On the other hand, given the fact that other constants are case-sensitive, this might make for more rather than less confusion as programmers forget which constants to treat in which way. Unless you have a compelling reason to do otherwise, the safest course is probably to keep your constants case-sensitive and define them using uppercase characters, which is an easy-to-remember convention.

Predefined Constants

PHP automatically provides some built-in constants for you. __FILE__, for example, returns the name of the file that the PHP engine is currently reading. __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 need version information included in script output when sending a bug report.

    Part III: Getting Involved with the Code