PHP const differs from define, Phpconstdefine
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
//
usually defines constants outside the class
define ("PHP", "111cn.net "
);
class The value of the
myclass{
//
constant will always remain the same. You do not need to use the $ symbol
const
constant = ' constant value '
when defining and using constants;
function
showconstant () {
echo self::
constant . "
"
; }}
echo MyClass::
constant . "
"
;
$classname = "MyClass"
;
Echo
$classname ::
constant . "
";
//
after PHP 5.3.0
$class =
new
MyClass ();
$class ->
showconstant ();
Echo
$class ::
constant . "
";
//
PHP 5.3.0 after//print_r (Get_defined_constants ()); You can use Get_defined_constants () to get all the 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.
http://www.bkjia.com/PHPjc/1102851.html www.bkjia.com true http://www.bkjia.com/PHPjc/1102851.html techarticle PHP const and define differences, Phpconstdefine original address: http://www.manongjc.com/article/491.html const is used for the definition of class member constants, after the definition can not be changed, and define we decided ...