The difference between define () and const:
Define () defines constants during the execution period, while Const defines constants at compile time. This will have a slight speed advantage (ie a slightly better performance), but it is not worth considering unless you are building a large, high concurrency system.
Define () puts constants into the global scope, even though defining constants using the Define method in a namespace is also a global scope. You cannot use define () to define class constants (class constants are defined using const), and constants within the namespace scope use const definitions such as: namespace const abc= ' 100′;
Define () allows you to use an expression in a constant name and a constant value, but no const is allowed. This makes define () more flexible.
Define () can be invoked in the if () code block, but not const.
Under the same scope, the define () constant name and const-defined constant name cannot be the same.
Const can define class constants and namespace constants. Such as
Namespace ABC; Const ABC = ' a '; Class Hello {Const C_NUM = 8;}
The code is as follows |
Copy Code |
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.
The code is as follows |
Copy Code |
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.
The code is as follows |
Copy Code |
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
The code is as follows |
Copy Code |
Define (' FOO ', ' BAR ', true); Www.111cn.net Echo FOO; BAR echo foo; BAR |
Summarize:
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.