Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book


Variables offer a flexible way of storing data because you can change their values and the type of data they store at any time during the execution of your scripts. However, if you want to work with a value that must remain unchanged throughout your script’s execution, you can define and use a constant. You must use PHP’s built-in define() function to create a constant, which subsequently cannot be changed unless you specifically define() it again. To use the define() function, place the name of the constant and the value you want to give it, within parentheses and separated by a comma:

define("YOUR_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 5.4 shows you how to define and access a constant.

Listing 5.4. Defining and Accessing a Constant

1: <?php
2: define("THE_YEAR", "2008");
3: echo "It is the year ".THE_YEAR;
4: ?>

Notice that in line 3 the concatenation operator is used to append the value held by the constant to the string "It is the year " because PHP does not distinguish between a constant and a string within quotation marks.

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

It is the year 2008

The define() function can also accept a third Boolean argument that determines whether the constant name should be case sensitive. By default, constant names are case sensitive. However, by passing true to the define() function, you can change this behavior, so if you were to set up our THE_YEAR constant as

define("THE_YEAR", "2008", true);

you could access its value without worrying about case:

echo the_year;
echo ThE_YeAr;
echo THE_YEAR;

The preceding three expressions are equivalent, and all would result in an output of 2008. This feature can make scripts a little friendlier for other programmers who work with our code because they will not need to consider case when accessing a constant we have already 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 to keep your constants case sensitive and define them using uppercase characters, which is an easy-to-remember (not to mention standard) convention.

Predefined Constants

PHP automatically provides some built-in constants for you. For example, the constant __FILE__ returns the name of the file that the PHP engine is currently reading. The constant __LINE__ returns the current 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 the PHP_VERSION constant. This constant can be useful if you need version information included in script output when sending a bug report.

  • + Share This
  • 🔖 Save To Your Account