The define () function defines a constant.
The define () function defines a constant.
Constants are much like variables, except t for the following differences:
Constants [constant] and variables [variable] have many similarities, so they are easy to be confused. Next, let's list the differences between constants [constant] and variables [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 you set a constant, you do not need to add the "$" symbol before it;
• Constants can be accessed regardless of scope
Constants can be accessed by all fields;
• Constant values can only be strings and numbers
The constant value can only be "string [string]" and "number [number]";
Syntax
Syntax
Copy codeThe Code is as follows: define (name, value, case_insensitive)
Parameter Parameters |
Description Description |
| Name |
Required. Specifies the name of the constant Required parameter. Constant name |
| Value |
Required. Specifies the value of the constant Required parameter. Constant Value |
| Case_insensitive |
Optional. Specifies whether the constant name shocould be case-insensitive. If set to TRUE, the constant will be case-insensitive. Default is FALSE (case-sensitive) Optional. Specifies whether the constant name is case-insensitive [case-insensitive]. If it is set to True, It is case-insensitive. If it is set to False, it is case-sensitive. The default value is False. |
Example 1
Case 1
Define a case-sensitive constant:
Specify a constant (case sensitive ):
Copy codeThe Code is as follows: <? Phpdefine ("GREETING", "Hello you! How are you today? "); Echo constant (" GREETING ");?>
The output of the code above will be:
The above code will output the following results:Copy codeThe Code is as follows: Hello you! How are you today?
Example 2
Case 2
Define a case-insensitive constant:
Specify a constant (Case Insensitive ):Copy codeThe Code is as follows: <? Phpdefine ("GREETING", "Hello you! How are you today? ", TRUE); echo constant (" greeting ");?>
The output of the code above will be:
The above code will output the following results:Copy codeThe Code is as follows: Hello you! How are you today?
The defined () function checks whether a constant exists.
The role of the defined () function is to check whether a constant exists.
Returns TRUE if the constant exists, or FALSE otherwise.
If the constant exists, True is returned. If the constant does not exist, False is returned.
Syntax
Syntax
Copy codeThe Code is as follows: defined (name)
Parameter Parameters |
Description Description |
| Name |
Required. Specifies the name of the constant to check Required parameter. Constant Object Name |
Example
Case
Copy codeThe Code is as follows: <? Phpdefine ("GREETING", "Hello you! How are you today? "); Echo defined (" GREETING ");?>
The output of the code above will be:
The above code will output the following results:Copy codeThe Code is as follows: 1