When defining constants in PHP, you can use both the const and define methods, so what's the difference between them?
1.const the definition of a class member variable, once defined, cannot be modified. 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 You cannot define constants in a conditional statement.
such as: if (constition) {
The philosophy can not use const, can only use define
}
4.const can only accept static scalars, and define can take any expression.
Const BIT_5 = 1 << 5; Invalid
Define (' Bit_5 ', 1 << 5); Valid
5.const defines a constant case-sensitive, while define can specify case sensitivity by using the third argument (true for case insensitivity).
The 6.const takes a common constant name. Define can use expressions as constants.
7. Using const makes the code easy to read, and const itself is a language structure. and define is a function.
8.const is much faster at compile time than define.
The difference between const and define in PHP