A constant is a simple identifier. This value cannot be changed during script execution (in addition to the so-called magic constants, they are not constants). Constants are case sensitive by default. Usually, constant identifiers are always uppercase.
You can use the Define () function to define constants. After php5.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 specify its name to get the value of a constant, 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_contstants ().
Constants and variables have the following differences:
1. No dollar sign ($) in front of constants
2. Constants can only be defined by the Define () function, not by an assignment statement
3. Constants can be defined and accessed anywhere, regardless of the scope of the variable
4. Once a constant is defined, it cannot be redefined or de-defined.
5, the value of the constant can only be scalar
<?php
The following code will work correctly after PHP 5.3.0
Const USERNAME = ' Zhouguowei ';
Echo USERNAME;
ECHO constant ("USERNAME");
Const ZHOUUSERNAME = ' zhouguowei2222222222 ';
Define (' MyUserName ', ' zhouguowei1111111 ');
echo "<pre>";
Print_r (Get_defined_constants ());
?>
Q: What is the difference between const and define when defining constants in PHP?
A: 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 for the definition of class member variables, once defined, can not be modified. Define cannot be used for definitions of class member variables and can be used for global constants.
2, the const can be used in the class, define cannot
3. Const can no longer define constants in conditional statements
<?php
if (...) {
Const FOO = ' BAR '; Invalid invalid
}
if (...) {
Define (' FOO ', ' BAR '); The effective valid
}
?>
4, const uses the ordinary constant name, define can use the expression as the name
<?php
Const FOO = ' BAR ';
for ($i = 0; $i < + + + $i) {
Define (' Bit_ '. $i, 1 << $i);
}
?>
5, const can only accept static scalar, and define can use any expression
<?php
Const BIT_5 = 1 << 5; Invalid invalid
Define (' Bit_5 ', 1 << 5); The effective valid
?>
6, const-defined constants are case-sensitive, and define can specify case sensitivity by the third argument (true for case insensitivity).
<?php
Define (' FOO ', ' BAR ', true);
Echo FOO; BAR
echo foo; BAR
?>
PHP Constants in detail: the difference between define and const