This section describes what is a constant, define() function defines a constant name to a value, constant value can retrieved by the name directly or by the constant() function, any string can be used as constant name.
What Is a Constant?
A constant is an identifier for a data value. Once defined, the data value identified by a constant can not be changed.
PHP supports constants with following basic rules:
1. A constant must be defined with the define(name, value).
For example, define("PI", 3.14159); defines a constant named as PI to a value of 3.14159.
2. To retrieve the value defined in a constant, you can use the constant name directly in any expression.
For example, define("PI", 3.14159); $area = PI * $radius * $radius; uses the value define in PI to calculate the area.
3. You are allowed to use any string for the constant name. For example, define("LONG PI", 3.14159265359);
defines a constant named as 'LONG PI' to a value of 3.14159265359. But using special characters in constant names
are not recommended.
4. If a constant name contains special characters, you need to use the constant(name) function to retrieve
the value defined in the constant. For example, define("LONG PI", 3.14159265359);
$area = constant("LONG PI") * $radius * $radius; uses the value defined in "LONG PI" to calculate the area.
5. If you want to know if there is a constant defined for a given constant name,
you can use the define(name) function. For example, define("LONG PI", 3.14159265359); defined("LONG PI"); returns TRUE.
And defined("LONG RI") returns FALSE.
6. Constants can be defined with only value of scalar data types: boolean, integer, float, and string.
7. The PHP engine provides a number of prefined (built-in) constants. For example,
__FILE__, __LINE__, PHP_OS, PHP_VERSION, PHP_INT_MAX, DIRECTORY_SEPARATOR, ...
Now let's look at my tutorial example below to confirm some rules mentioned above:
<?php # ConstantTest.php
# Copyright (c) 2005 by Dr. Herong Yang. http://www.herongyang.com/
#
print "\n PI is not defined as a constant:\n";
print " PI: "; var_dump(PI);
define("PI", 3.14159);
print "\n PI is defined to a constant now:\n";
print " PI: "; var_dump(PI);
define("LONG PI", 3.14159265359);
print "\n LONG PI is defined to a constant now:\n";
print " LONG PI: "; var_dump(constant("LONG PI"));
print "\n LONG RI is not defined as a constant:\n";
print " LONG PI: "; var_dump(defined("LONG PI"));
print " LONG RI: "; var_dump(defined("LONG RI"));
print "\n Trying to define DAYS to an array:\n";
define("DAYS", array("Jan"=>31,"Feb"=>28));
print " DAYS: "; var_dump(DAYS);
print "\n PHP built-in constants:\n";
print " __FILE__: "; var_dump(__FILE__);
print " __LINE__: "; var_dump(__LINE__);
print " PHP_OS: "; var_dump(PHP_OS);
print " PHP_VERSION: "; var_dump(PHP_VERSION);
print " PHP_INT_MAX: "; var_dump(PHP_INT_MAX);
print " DIRECTORY_SEPARATOR: "; var_dump(DIRECTORY_SEPARATOR);
?>
If you run this sample script, you should get:
PI is not defined as a constant:
PI: string(2) "PI"
PI is defined to a constant now:
PI: float(3.14159)
LONG PI is defined to a constant now:
LONG PI: float(3.14159265359)
LONG RI is not defined as a constant:
LONG PI: bool(true)
LONG RI: bool(false)
Trying to define DAYS to an array:
Warning: Constants may only evaluate to scalar values in
C:\herong\php_book\ConstantTest.php on line 20
DAYS: string(4) "DAYS"
PHP built-in constants:
__FILE__: string(43) "C:\herong\php_book\ConstantTest.php"
__LINE__: int(25)
PHP_OS: string(5) "WINNT"
PHP_VERSION: string(5) "5.0.4"
PHP_INT_MAX: int(2147483647)
DIRECTORY_SEPARATOR: string(1) "\"