Constants can be understood as values that are invariant. After a constant value is defined, it cannot be changed anywhere else in the script. A constant is made up of English letters, underscores, and numbers, but numbers cannot appear as first letters.
Use the Defaine () function in PHP to define constants, which have the syntax format:
Define (string constant_name, mixed value, case_sensitive = True)
The function has 3 parameters:
Constant_name: Required parameter, constant name, that is, marker
Value: Required arguments, values of constants
Case_sensitive: Optional parameter, specifying whether case sensitive, set to TRUE indicates insensitive
There are two ways to get constant values:
1. Use the constant name to get the value directly;
2. Use the constant () function.
The constant () function and the direct use of the constant name output is the same, but the function can dynamically output different constants, in the use of flexible and convenient.
The syntax format is:
Mixed constant (string constant_name)
The parameter constant_name is the name to get the constant, or a variable that stores the constant name.
If successful, the value of the constant is returned, and the failure indicates that the error message constant is not defined.
To determine whether a constant has been defined using the defined () function. The syntax format for a function is:
BOOL Defained (String constants_name)
Constant_name to get the name of the constant, the existence returns TRUE, otherwise it returns false;
You can use predefined constants in PHP to get information in PHP. such as "_file_", "_line_", "Php_os" and so on.
Cases:
<?php
Define ("message", "PHP constant definition, constant name difference case");
echo message. " <br/> "; Output constant Message
echo message. " <br/> "; Output "message", indicating that there is no such constant
Define ("MESSAGE2", "PHP constant definition, constant name not case-sensitive", true);
echo MESSAGE2. " <br/> "; Output Constants MESSAGE2
echo Message2. " <br/> "; Output constants
$constant _name = "Message2";
ECHO constant ($constant _name). " <br/> "; Output Constants MESSAGE2
echo defined ("message"). " <br/> "; If the definition returns True,echo output displays 1
?>