Constants can be understood as values unchanged, constants are defined and cannot be changed anywhere else in the script, a constant consists of an English letter, an underscore, and a number, but the number cannot be used as the first letter of a constant.
Use the # define () function in PHP to define constants, which are syntactically formatted as follows:
Define (string constant_name,mixed value,case_sensitive=true)
There are two ways to get the value of a constant, one is to get the value directly using the name of the constant, the second is to take advantage of the constant () function, the second is to use the constant () function and the effect of the output directly using the constant name, but the function can dynamically output the values of different constants, This is much more flexible and convenient.
The first gets its value directly with the name of the constant
<?php define ("test", "I am jack!"); echo test;?>
The results of the operation are as follows:
The second use of the constant () function to get the value code for the constant is as follows:
<?php define ("test", "I am jack!"); $JACKSTR = "Test"; ECHO constant ($JACKSTR);?>
The results of the operation are as follows:
Then let's talk about the case-sensitive problem, just the declaration of the constant is case-sensitive, we use the code to verify the see is not, in addition to enhance my memory
The code is as follows:
<?php define ("test", "I am jack!", true); echo Test;echo "\ n"; echo Test;echo "\ n"; echo Test;echo "\ n";?>
Let's look at the results of the output again.
You can find that the settings are not sensitive to case, even if the case is different, but the output is still the same.
The predefined constants are the same as the constants we define ourselves, with the usual predefined constants:
Declaration and use of PHP constants