Difference between php const and define, phpconstdefine
Address: http://www.manongjc.com/article/491.html
Const is used for the definition of Class Member constants. It cannot be modified after definition, and define is used to define global constants, so that we can access but cannot change it elsewhere, here are some details for you.
Note: define cannot be defined in the class, and const must be defined in the class, and const must be accessed through the Class Name: variable name.
1. const is used for defining class member variables. Once defined, the value cannot be changed. Define defines global constants that can be accessed anywhere.
2. define cannot be defined in the class, But const can.
3. const cannot define constants in condition statements.
4. const uses a normal constant name, and define can use an expression as the name.
5. const can only accept static scalar, while 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 const is simple and easy to read. It is a language structure, and define is a method. Using const to define it is much faster than define during compilation.
Define defines constants. What if a constant is defined in a class? Of course, define cannot be used, but const can be used, for example:
<? Php // usually defines the constant define ("PHP", "111cn.net") outside the class. The value of the class MyClass {// constant will remain unchanged. When defining and using constants, you do not need to use the $ symbol const constant = 'constant value'; function showConstant () {echo self: constant. "<br>" ;}} echo MyClass: constant. "<br>"; $ classname = "MyClass"; echo $ classname: constant. "<br>"; // $ class = new MyClass (); $ class-> showConstant (); echo $ class: constant. "<br>"; // PHP 5.3.0 and later // print_r (get_defined_constants (); // you can use get_defined_constants () to get all defined constants?>
Generally, define defines constants outside the class, const defines constants within the class, and const must be accessed by Class Name: variable name. However, php5.3 and above support defining constants through const outside the class, as shown below, it is OK:
<?php const a = "abcdef"; echo a;?>
I will not talk about the basic knowledge of constants here. Apart from the above, other differences between define and const (from the network ):
1. const cannot define constants in condition statements, but define is acceptable, as follows:
<? Php if (1) {const a = 'java';} echo a; // required?>
2. const uses a normal constant name, and define can use an expression as the name
<?phpconst FOO = 'PHP'; for ($i = 0; $i < 32; ++$i) { define('PHP_' . $i, 1 << $i); } ?>
3. const can only accept static scalar, while define can use any expression.
<? Phpconst PHP = 1 <5; // error define ('php', 1 <5); // correct?>
4. const itself is a language structure. Define is a function. Therefore, using const is much faster.
Two common steps: Both of them cannot be re-assigned.