A constant can be interpreted as a constant value. Once defined, constants cannot be changed anywhere in the script.
What is a constant?
A constant can be interpreted as a constant value. Once defined, constants cannot be changed anywhere in the script.
Constant definition method
Constants are composed of letters, underscores, and numbers. However, note that the first letter of a constant cannot start with a number.
In PHP, we use the define () function to define constants. the syntax of the define () function is as follows:
bool define ($name , $value ,$case_insensitive = false)
Parameter details
This function has three parameters:
$ Name: constant name, which must have
$ Value: constant value, which must have
$ Case_insensitive is used to control whether the constant name is case sensitive (sensitive by default). if it is set to true, it indicates no sensitivity. Optional. It has two values: true and false.
Get constants
You can use either of the following methods to obtain the value of a constant:
The other is to use the constant () function. the effect of the constant () function is the same as that of the constant name, but the function can dynamically output different constants, it is much more flexible and convenient to use.
The syntax format of the constant () function is as follows:
constant (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 result is obtained successfully, the constant value is returned. Otherwise, the error message constant is not defined.
Code running result:
Judge whether a constant is defined
To determine whether a constant has been defined, you can use the defined () function. the syntax format of the function is as follows:
defined(name)
If the parameter name is the name of the constant to be obtained, true is returned. otherwise, false is returned.
Code running result:
Instance
To better understand how to define constants, an example of defining constants is provided here. Use the define () function, constant () function, and defined () function in the instance. Use the define () function to define a constant, use the constant () function to dynamically obtain the value of a constant, and use the defined () function to determine whether a constant is defined. The instance code is as follows:
"; // The output constant GREETINGecho Greeting ."
"; Define ('count', 'can see PHP Chinese network Multiple Times', true); echo COUNT ."
"; // Output constant COUNTecho Count ."
"; // The output constant COUNT because it is case insensitive $ name = 'count'; echo constant ($ name )."
"; // The output constant COUNTecho (defined ('greeting '))."
"; // If a constant is defined, true is returned. 1?>
Code running result:
The above is a simple example of defining, obtaining, and judging constants through functions. in the next section, we will explain the "predefined constants" in PHP ".
Related video tutorial recommendations: php1.cn solitary nine cheap (4)-php Video Tutorial: variables and constants: small warehouses for storing data in programs.
The above is a detailed description of the definition and use of PHP constants. For more information, see other related articles in the first PHP community!