1. Const is used for class member variable definition once defined and cannot change its value. Define defines global constants that can be accessed anywhere.
2, define can not be defined in the class and Const.
3. Const cannot define constants in conditional statements
Copy Code code as follows:
if (...) {
Const FOO = ' BAR '; Invalid
}
But
if (...) {
Define (' FOO ', ' BAR '); Valid
}
4, the const use a common constant name, define can use an expression as the name.
Copy Code code as follows:
Const FOO = ' BAR ';
for ($i = 0; $i < + + $i) {
Define (' Bit_ '. $i, 1 << $i);
}
5, const can only accept static scalar, and define can take any expression.
Copy Code code as follows:
Const BIT_5 = 1 << 5; Invalid
But
Define (' Bit_5 ', 1 << 5); Valid
6. Const is always case sensitive, but define () can define a case insensitive constant with a third argument
Copy Code code as follows:
Define (' FOO ', ' BAR ', true);
Echo FOO; BAR
echo foo; BAR
Summary:
Using const is easy to read, it is a language structure, and define is a method that is defined at compile time much faster than define.