Original address: http://www.manongjc.com/article/491.html
Const is the definition of a class member constant, which cannot be changed after the definition, and define we define global constants so that we can access them elsewhere but not change them.
Note: Define cannot be defined in a class, and a const must be defined in a class, and the const must be accessed through the class name:: Variable name
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
4, const uses a common constant name, define can use the expression as the name.
5, const can only accept static scalar, and define can use any expression.
6, const is always case sensitive, however define () can use the third parameter to define a case insensitive constant
7. Using the const is easy to read, it is itself a language structure, and define is a method that uses the const definition at compile time much faster than define.
Define is a constant defined, what if a constant is defined in the class? Of course not with define, but with Const, as in the following example:
Php//defining constants outside the class is usually the caseDefine("PHP", "111cn.net");classmyclass{//the value of the constant will always remain unchanged. You do not need to use the $ symbol when defining and using constantsConstconstant= ' constant value '; functionshowconstant () {EchoSelf::constant. "
"; }} EchoMyClass::constant. "
"; $classname= "MyClass";Echo$classname::constant. "
";//after PHP 5.3.0$class=NewMyClass ();$class-showconstant ();Echo$class::constant."
";//PHP 5.3.0 after//print_r (Get_defined_constants ()); All defined constants can be obtained with get_defined_constants ()?>
Typically define defines constants outside the class, const defines constants within the class, and const must be accessed through the class name:: Variable name. But php5.3 above support class through Const definition constants, see below, this is OK:
PHP const A = "abcdef"; Echo A;? >
The basics of constants are not mentioned here, except above, the other differences between define and const (excerpt from the network):
1.const cannot define constants in conditional statements, but define is possible, as follows:
PHP If(1) { const A = ' java '; } Echo A; // must be wrong ?>
2.const takes an ordinary constant name, define can use an expression as the name
PHPconst FOO = ' php ' for ($i$i < + +$i ) { define$i$i?>
3.const can only accept static scalars, and define may take any expression.
PHPconst php = 1 << 5; // Error Define // ?>
4.const itself is a language structure. and define is a function. So it's much faster to use the const.
Two co-sync: both cannot be re-assigned.
The above describes the PHP const and define differences, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.