In the previous article, we introduced the use of define() in php, and the difference between define() and const in php. Today we will continue to introduce the define() function and defined() function in php. The usage is analyzed and introduced in detail, please refer to it if you need it!
The define() function defines a constant.
The define() function defines a constant.
Constants are much like variables, except for the following differences:
Constant [constant] has many similarities with variables [variable], so it is easy to confuse; below, we list the constants [constant] The difference between ] and variable [variable]:
•A constant's value cannot be changed after it is set
A constant value cannot be changed after it is specified;
•Constant names do not need a leading dollar sign ($)
When setting constants, there is no need to add the "$" sign in front;
•Constants can be accessed regardless of scope
Constant values can only be strings and numbers
Constant values can only be strings and numbers String
[string]" and "number[number]";
Syntax
Syntax
The code is as follows:
define(name,value,case_insensitive)
Example 1Case 1Define a case-sensitive constant:
Specify a constant (case sensitive):
The code is as follows:
<?phpdefine("GREETING","Hello you! How are you today?");echo constant("GREETING");?>
The above code will output the following results:
Hello you! How are you today?
Case 2
Define a case-insensitive constant:Specify a constant (case-insensitive):<?phpdefine("GREETING","Hello you! How are you today?",TRUE);echo constant("greeting");?>
The above code will output the following results:
Hello you! How are you today?
The defined() function checks whether a constant exists.
defined() The function is to check whether a constant exists.
Returns TRUE if the constant exists, or FALSE otherwise.
If the constant exists, it returns True; if it does not exist, it returns False.
Syntax
Syntaxdefined(name)
Description Description | |
---|---|
Required parameters. Specify the name of the constant | object