A constant is an identifier (first name) of a simple value. As the name implies, the value cannot be changed during script execution (except for the so-called magic constants, they are not constants). Changshime is considered to be case sensitive. Usually, constant identifiers are always uppercase.
You can use the Define () function to define constants. After PHP 5.3.0, you can use the Const keyword to define constants outside the class definition, and the previous version of the Const keyword can only be used in class. Once a constant is defined, it can no longer be changed or undefined.
Constants can only contain scalar data (boolean,integer,float and string). You can define resource constants, but avoid them as much as possible, because they cause unpredictable results.
You can simply get the value of a constant by specifying its name, and unlike a variable, you should not precede the constant with the $ symbol. If the constant name is dynamic, you can also use the function constant () to get the value of the constant. A list of all defined constants can be obtained with get_defined_constants ().
Constants and variables have the following differences:
- The constant is preceded by a dollar sign ($);
- Constants can only be defined with the Define () function, not through assignment statements;
- Constants can be defined and accessed anywhere, regardless of the scope of the variable;
- Once a constant is defined, it cannot be redefined or undefined;
- The value of a constant can only be scalar.
Example #1 Defining constants
2 |
define("CONSTANT","Hello world."); |
3 |
echoCONSTANT;// outputs "Hello world." |
4 |
echoConstant;// 输出 "Constant" 并发出一个提示性信息 |
Example #2 use keyword const to define constants
2 |
// 以下代码在 PHP 5.3.0 后可以正常工作 |
3 |
constCONSTANT ='Hello World'; |
Example #3 Legal and illegal constant names
03 |
define("FOO", "something"); |
04 |
define("FOO2", "something else"); |
05 |
define("FOO_BAR","something more"); |
07 |
define("2FOO", "something"); |
08 |
// 下面的定义是合法的,但应该避免这样做:(自定义常量不要以__开头) |
09 |
// 也许将来有一天PHP会定义一个__FOO__的魔术常量 |
11 |
define("__FOO__","something"); |
When defining constants in PHP, the difference between const and define is:
Using const makes the code easy to read, and const itself is a language structure, and define is a function. In addition, const is much faster to compile than define.
(1). Const is used to define a class member variable, which is not modifiable once defined. Define is not available for definition of class member variables and can be used for global constants.
(2). Const can be used in a class, define cannot.
(3). Const cannot define a constant in a conditional statement.
For example:
2 |
constFOO = 'BAR'; // 无效的invalid |
5 |
define('FOO','BAR'); // 有效的valid |
(4). Const takes an ordinary constant name, and define can use an expression as its name.
2 |
for($i= 0; $i< 32; ++$i) { |
3 |
define('BIT_'. $i, 1 <<$i); |
(5). Const can only accept static scalars, and define may take any expression.
For example:
1 |
constBIT_5 = 1 << 5; // 无效的invalid |
2 |
define('BIT_5', 1 << 5);// 有效的valid |
(6). const-defined constants are case-sensitive, and define can specify case sensitivity by using the third argument (true for case insensitivity).
For example:
1 |
define('FOO','BAR', true); |
Related functions:
define-defines a constant
Description
BOOL Define (String $name, mixed $value [, bool $case _insensitive = false]
Parameters:
Name: constant name.
Value: constant, only scalar and null are allowed. The type of scalar is integer, Float,string, or Boolean. It is also possible to define a constant value of type resource, but this is not recommended and may result in an
The state of being aware of the occurrence.
Case_insensitive: If set to TRUE, the constant is case insensitive. The default is case-sensitive. For example, CONSTANT and CONSTANT represent different values. (Note: Case insensitive constants in lowercase
stored in the same way. )
Return value: Returns TRUE on success, or FALSE on failure.
constant-returns the value of a constant
Description
Mixed constant (string $name)
Returns the value of a constant by name. Constant () is useful when you do not know the constant name but need to get the value of the constant. That is, the constant name is stored in a variable, or the function returns a constant name. The function also applies
Class constants.
Parameters:
Name: constant name.
return value:
Returns the value of a constant. Returns NULL if the constant is undefined.
defined-checks if a constant exists for a name
Description
bool Defined (string $name)
Checks if the constant for the name is defined.
Note: If you want to check if a variable exists, use Isset (). The defined () function is only valid for constants. If you want to detect if a function exists, use function_exists ().
Parameters:
Name: The names of the constants.
return value:
Returns TRUE if the constant of the name is defined, or FALSE if undefined.
Get_defined_constants:
Returns an associative array with the names of all the constants and their values
Returns the value of a constant name and constant in an associative array. This includes those constants created by the extension and by the Define () function
The above describes the const and define differences in PHP (supplemental), including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.