1. Const is used for class member variable definition, once defined and cannot change its value. Define defines global constants that can be accessed everywhere.
2. Define cannot be defined in a class and const can.
3. Const cannot define constants in conditional statements
if (...) {
Const FOO = ' BAR '; Invalid
}
But
if (...) {
Define (' FOO ', ' BAR '); Valid
}
4, const uses a common constant name, define can use the expression as the name.
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.
Const BIT_5 = 1 << 5; Invalid
But
Define (' Bit_5 ', 1 << 5); Valid
6, const is always case sensitive, however define () can use the third parameter to define a case insensitive constant
Define (' FOO ', ' BAR ', true); Www.2cto.com
Echo FOO; BAR
echo foo; BAR
Summarize:
The use of const is simple and easy to read, it is itself a language structure, and define is a method, with a const definition at compile time much faster than define.
The difference between the use of const and define in PHP