JavaScript Editor Javascript source editor     Web programming 

Team LiB
Previous Section Next Section


Variables offer a flexible way of storing data, as 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 needs to remain unchanged throughout your script's execution, you can define and use a constant variable. 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, 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", "2004");
3: echo "It is the year ".THE_YEAR;
4: ?>

By the Way

When using constants, keep in mind they can be used anywhere in your scripts, including external functions which are included.

Notice that in line 3 we used the concatenation operator to append the value held by our 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:

It is the year 2004

The define() function can also 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 THE_YEAR constant as

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

we could access its value without worrying about case:

echo the_year;
echo ThE_YeAr;
echo THE_YEAR;

These expressions are all equivalent, and all would result in an output of 2004. This feature can make scripts a little friendlier for other programmers who work with our code, as they will not need to consider case when accessing a constant that 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 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 can be useful if you need version information included in script output when sending a bug report.

    Team LiB
    Previous Section Next Section

    JavaScript Editor Javascript source editor     Web programming