PHP Constants
A constant in PHP is an identifier whose value cannot be changed once it is defined.
Once a constant value is defined, it cannot be changed anywhere else in the script.
A constant can be defined using the define() function or the const keyword.
Characteristics of Constants
PHP Constants
- Immutability: Once a constant is defined, its value cannot be changed.
- Global scope: After a constant is defined, it can be used anywhere in the entire script without using
globalThe const keyword. - Data Type: The value of a constant can be a scalar data type (such as boolean, integer, float, string) or an array (PHP 7 and above).
- Case Sensitivity: Constant names are case-sensitive by default. If you need to define a case-insensitive constant, you can set
define()the third parameter of the function totrue.
A constant is an identifier for a simple value that cannot be changed during script execution.
A constant consists of English letters, underscores, and digits, but a digit cannot appear as the first character. (A constant name does not need the $ prefix.)
Note: Constants can be used throughout the entire script.
Setting PHP Constants
To set a constant, use the define() function, whose syntax is as follows:
bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )
This function has three parameters:
-
name:Required parameter: the constant name, i.e., the identifier.
-
value:Required parameter: the value of the constant.
-
case_insensitive : Optional parameter: if set to TRUE, the constant is case-insensitive; by default it is case-sensitive.
Note:Starting from PHP 7.3.0, defining case-insensitive constants has been deprecated. Starting from PHP 8.0.0, only false is an acceptable value, and passing true will produce a warning.
In the example below we create a case-sensitive constant(not recommended after PHP 7.3), whose value is "Welcome to Runoob.com":
Example
' ; echo greeting ; // output "greeting", but there is a warning indicating the constant is undefined ?>In the example below we create a case-insensitive constant, whose value is "Welcome to Runoob.com":
Example
Constants Are Global
After a constant is defined, it is a global variable by default and can be used anywhere in the running script.
The example below demonstrates using a constant inside a function; even if the constant is defined outside the function, it can still be used normally.
Example
Using the const Keyword
const CONSTANT_NAME = "value";
The following is an example of defining a constant using the const keyword:
Example
const
SITE_URL
=
"https://www.runoob.com"
;
echo
SITE_URL
;
// output "https://www.runoob.com"
Predefined Constants
PHP provides some predefined constants that can be used directly in scripts. These constants are typically used to obtain PHP configuration information, version information, etc. Common predefined constants include:
PHP_VERSION: The version of the current PHP parser.PHP_OS: The server's operating system.PHP_INT_MAX: The largest integer value.E_ERROR,E_WARNING,E_PARSEetc.: Error reporting levels.
Example
echo
PHP_VERSION
;
// output the PHP version, e.g. "7.4.1"
echo
PHP_OS
;
// output the operating system, e.g. "Linux"
echo
PHP_INT_MAX
;
// output the largest integer value, e.g. "9223372036854775807"
Constant Arrays (PHP 7 and above)
In PHP 7 and above, constants can also be arrays.
Example
define
(
"FRUITS"
,
[
"Apple"
,
"Banana"
,
"Orange"
]
)
;
echo
FRUITS
[
0
]
;
// output "Apple"
Or use const:
Example
const
COLORS
=
[
"Red"
,
"Green"
,
"Blue"
]
;
echo
COLORS
[
1
]
;
// output "Green"