How to declare PHP constants
Constants can be understood as unchanging quantities. Constants are defined and cannot be changed anywhere else in the script. A constant is made up of English letters, underscores, and math. But the numbers do not appear as initial letters.
Use the Define () function in PHP to define constants in the following syntax format:
Define (string constant_name,mixed value,case_sensitive=true)
Parameter description:
Constant_name |
Required parameters. The constant name, or identifier. |
Value |
Required parameters. The value of the constant. |
Case_sensitive |
Optional parameters. Specifies whether case-sensitive, which is set to true, indicates insensitivity. |
There are two ways to get constants: one is to use constants directly, the other is to use the constant () function, and the constant () function is the same as using the constant name directly. However, the function can dynamically output different constants, which is more flexible and convenient to use. The function syntax format is:
MIXDE constant (String const_name)
The parameter const_name is the name of the constant to get, or a variable that stores the constant name. If successful, returns a constant value, otherwise the error message constant is not defined.
To determine whether a constant has been defined, you can use the Define () function. The function syntax format is:
bool Defined (string constant_name)
The parameter constant_name is the name to get the constant, returns true successfully, otherwise false.
Examples of use of PHP constants:
To better understand how constants are defined, here is a definition of a constant instance. The define () function, the constant () function, and the defined () function are used in the example for 3 functions. Use the defined () function to define a constant, use the constant () function to dynamically get the value of the constant, and use the Define () function to determine whether the constant is defined. The code is as follows:
<?php define ("MESSAGE", "can see Once"); The Echo MESSAGE. <br > "; The Echo Message. <br > "; The output message indicates that there is no constant define ("COUNT", "can see multiple times", true); echo COUNT. " <br > "; echo CoUnT. " <br > "; Output count, set case insensitive $name = "Count"; Get The constant Echo constant ($name) by using the constant () function. " <br > "; Whether there is a constant "message" Echo (Defined ("message")). " <br > "; Returns true if the constant is defined, using the Echo output to display 1.?>
Output Result:
Can see one time notice:use of undefined constant message-assumed ' Message ' in editor code error location. The message can be seen multiple times to see multiple times 1