In PHP define () and const () can define constants, then the difference between define () and const exactly where, this many programmers do not understand, I would like to introduce to you some of this function usage ratio.
The difference between define () and const:
Define () defines constants at execution time, while Const defines constants at compile time. This way, Const has a slight speed advantage (i.e. performance is slightly better), but it is not worth considering this issue unless you are building a high concurrency system.
Define () puts constants into the global scope, even if the constants are defined in the namespace using the Define method. You cannot use define () to define class constants (class constants use the const definition), and constants within namespace scopes are defined using a const definition such as: namespace const abc= ' 100′;.
Define () allows you to use expressions in constant names and constant values, which are not allowed by Const. This makes define () more flexible.
Define () can be called in the If () code block, but the const is not.
Under the same scope, the define () constant name and the const definition cannot be the same name.
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, const uses a common constant name, define can use the 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 use 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, however define () can use the third parameter to define a case insensitive constant
The code is as follows |
Copy Code |
Define (' FOO ', ' BAR ', true); www.bKjia.c0m 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.
http://www.bkjia.com/PHPjc/632646.html www.bkjia.com true http://www.bkjia.com/PHPjc/632646.html techarticle in PHP define () and const () can define constants, then the difference between define () and const exactly where, this many programmers do not understand, I would like to introduce you to some of this function ...