A constant can be interpreted as a constant volume. After a constant is defined, it cannot be changed anywhere in the script. A constant consists of letters, underscores, and mathematics. However, a number cannot appear as the first letter. Use the define () function in PHP to define constants. the syntax format is as follows:
PHP constant declaration method
A constant can be interpreted as a constant volume. After a constant is defined, it cannot be changed anywhere in the script. A constant consists of letters, underscores, and mathematics. However, a number cannot appear as the first letter.
Use the define () function in PHP to define constants. the syntax format is as follows:
define(string constant_name,mixed value,case_sensitive=true)
Parameter description:
Constant_name |
Required parameter. Constant name, that is, identifier. |
Value |
Required parameter. Constant value. |
Case_sensitive |
Optional. Specifies whether it is case sensitive. if it is set to true, it indicates no sensitivity. |
There are two ways to get a constant: one is to get a constant directly; the other is to use the constant () function, and the constant () function outputs the same effect as directly using the constant name. However, functions can dynamically output different constants, making them more flexible and convenient to use. Function syntax format:
mixde constant(string const_name)
The const_name parameter is the name of the constant to be obtained. It can also be a variable that stores the constant name. If the query succeeds, a constant value is returned. Otherwise, the error message constant is not defined.
To determine whether a constant has been defined, use the define () function. Function syntax format:
bool defined(string constant_name)
The constant_name parameter is the name of the constant to be obtained. if the constant is successful, true is returned. otherwise, false is returned.
PHP constant usage example:
To better understand how to define constants, a constant definition instance is provided here. The preceding define () function, constant () function, and defined () function are used in the instance. Use the defined () function to define a constant, use the constant () function to dynamically obtain the value of a constant, and use the define () function to determine whether a constant is defined. The code is as follows:
"; Echo Message ."
"; // Output Message, indicating that the constant define (" COUNT "," can be seen multiple times ", true); echo COUNT ."
"; Echo CoUnT ."
"; // Output COUNT, case insensitive $ name =" count "; // Obtain the constant echo constant ($ name) through the constant () function )."
"; // Whether the constant" MESSAGE "echo (defined (" MESSAGE ") exists "))."
"; // If a constant is defined, true is returned. 1 is displayed using echo output.?>
Output result:
You can see the error location of the Notice: Use of undefined constant Message-assumed 'message' in editor code. Message can be seen multiple times.
The above is a detailed description of PHP constant declaration and usage-php constant definition method. For more information, see other related articles in the first PHP community!