- 4.1 Data Types
- 4.2 Variables
- 4.3 Constants
- 4.4 Chapter Summary
- Chapter 4 Lab
4.3 Constants
"The only thing constant in life is change."
—Francois de la Rouchefoucauld, French classical author
Some real-world constants, such as pi, the speed of light, the number of inches in a foot, and the value of midnight, are values that don't change. PHP not only provides its own predefined constants but lets you create your own. Using constants makes it easy to write and maintain your programs.
4.3.1 What Is a Constant?
Unlike variables, a constant is a value that, once set, cannot be changed or unset during the execution of your script. An example of a constant is the value of pi or the version of PHP you are using. Constants are very useful because they are visible throughout a program (global in scope) and their values don't change; for example, a constant might be defined for the document root of your server, the name of your site, or the title, author, and copyright year of this book. Once defined, those values are fixed.
You can define constants at the top of your program or in another file that can be included in your script. (See the require() and include() functions discussed in "Managing Content with Include Files" on page 487.) Later if a constant value needs to be modified, once you change its value in the program, then when the program is executed, the new value will be reflected wherever the constant is used throughout the program, thus facilitating program maintenance.
4.3.2 Creating Constants with the define() Function
PHP constants are defined as words, and by convention, capitalized. Like variables, they are case sensitive and consist of uppercase and lowercase letters, numbers, and the underscore. Like variables, they cannot start with a number.
Unlike variables, constants are not preceded by a dollar sign and are not interpreted when placed within quotes.
Constants are global in scope, meaning they are available for use anywhere in a PHP script.
The only way that you can create a constant is with he PHP built-in define() function. Only a single, scalar value can be assigned to a constant, including strings, integers, floats, and booleans.
The define() function creates a named constant. The first argument is the name of the constant and the second argument is the value that will be assigned to it. Constants are normally case sensitive, but you can use an optional third argument of TRUE to turn off case sensitivity.
Format
bool define ( string name, mixed value [, bool case_insensitive] )
Example:
// defines document root define( 'DOC_ROOT', '/http://artemis/~ellie/public_html' ); // defines the include folder define( 'INCLUDES', DOC_ROOT.'/../includes' );
Example 4.25.
<?php 1 define('ISBN', "0-13-140162-9"); 2 define('TITLE', "JavaScript by Example" ); 3 if ( defined('ISBN') and defined('TITLE')){ 4 print ISBN . "<br />"; print TITLE . "<br />"; } 5 define('TITLE', "PHP by Example"); // Can't change TITLE, and // can't redefine it. 6 print TITLE; ?>
Explanation
1, 2 |
Two constants are defined, ISBN and TITLE, the first argument to the function. The second argument is the value being assigned to each of the constants. Once set, the only way to change a constant is to redefine it with the define() function. |
3 |
The define() function returns TRUE if the named constant has been defined. The expression reads, "if the constant ISBN and the constant TITLE have both been defined, proceed to line 3." |
4 |
Notice that the constants are not quoted. If they are quoted, their values will not be printed, but just the words ISBN and TITLE. |
5 |
You cannot redefine a constant like this. If you want to modify the value, you must go back into the program and change the original definition on line 2. |
6 |
The constant TITLE was unaffected by line 5. By definition, a constant cannot be changed or unset. The output of this program is shown in Figure 4.30. |
Figure 4.30 User-defined constants. Output from Example 4.25.
The defined() function checks whether a constant has been set. It returns TRUE if the constant has been defined; otherwise, FALSE.
4.3.3 The constant() Function
The constant() function returns that value of a constant. This function can be helpful if you don't know the name of the constant because its name was stored in a variable or was returned from a function.
Format
mixed constant ( string name )
Example:
define (ISBN, "0-13-140162-9"); $value=constant(ISBN); // Returns 0-13-140162-9
4.3.4 Predefined and "Magic" Constants
PHP comes with a number of predefined constants shown in Table 4.6. They provide information that doesn't change such as the name of the script file, the version of PHP and the operating system, and so on.
There are five predefined constants called magic constants (see Table 4.7). These are constants that change depending on how they are used in a program. They cannot be enclosed in quotes and are not case sensitive. The name of the contant is enclosed in two underscores on both sides.
Table 4.7. Magic Constants
Name |
Description |
__LINE__ |
The current line number of the file. |
__FILE__ |
The full path and filename of the file. If used inside an include, the name of the included file is returned. |
__FUNCTION__ |
The function name (added in PHP 4.3.0). As of PHP 5 this constant returns the function name as it was declared (case sensitive). In PHP 4 its value is always lowercased. |
__CLASS__ |
The class name (added in PHP 4.3.0). As of PHP 5 this constant returns the class name as it was declared (case sensitive). In PHP 4 its value is always lowercased. |
__METHOD__ |
The class method name (added in PHP 5.0.0). The method name is returned as it was declared (case sensitive). |
PHP has several special built-in constants described in Table 4.8.
Table 4.8. Built-In Constants
Name |
Description |
PHP_VERSION |
The version of the PHP parser currently running |
PHP_OS |
The operating system of the server on which the PHP parser is running |
PHP_OS |
The name of the operating system on which the PHP parser is executing; e.g., Linux |
TRUE |
A true value. |
FALSE |
A false value. |
The script in Example 4.26 shows how the predefined constants can be used to give information to the browser. It's output is displayed in Figure 4.31.
Example 4.26.
<?php // Using PHP built-in constants echo "PHP version = " . PHP_VERSION . "<br />"; echo "Server operating system = " . PHP_OS . "<br />"; echo "Current file name= " . __FILE__ . "<br />"; echo "Current line number= " . __LINE__ . "<br />"; echo "TRUE = ". TRUE . "<br />"; echo "false = ". FALSE . "<br />"; ?>
Figure 4.31 Predefined constants. Output from Example 4.26.