Constants
1, the composition of the constant = constant Name + constant value
Constant name: A combination of [a-za-z0-9_], and the number can not be started, case-sensitive, recommended capitalization
Constant value: Assignable data type integral type, float, Boolean, string, NULL
2. How to declare a constant
Define (' Constant name ', ' constant value ')
const constant NAME = constant Value (supported after PHP 5.3)
3, the constant detection bool defined (constant name)
4, the characteristics of the constant:
(1) Once defined, cannot be re-declared
(2) Once defined, the value cannot be re-assigned
5. Variable constant constant (constant name)
6, define and const difference
(1) can declare "location"
Define: Line of code, control structure, loop structure, function
Const: Line of code, class
Note:
Line of code: not control structure, loop body, function, class, Pure line of code
Constants declared with define in a function can only be used after a function call, otherwise an error will be made when the string output
(2) Scope
Constants declared by define and const in code lines, with global validity
Const constants declared in a class can only be used in a class
(3) const takes an ordinary constant name, define can use an expression as the name
Const FOO = ' BAR ';
for ($i = 0; $i < + + + $i) {
Define (' Bit_ '. $i, 1 << $i);
}
(4) const can only accept static scalar, and define may take any expression
For example:
Const BIT_5 = 1 << 5; Invalid invalid
Define (' Bit_5 ', 1 << 5); The effective valid
(5) const-defined constants are case-sensitive, while define can specify case sensitivity by using the third argument (true for case insensitivity)
For example:
Define (' FOO ', ' BAR ', true);
Echo FOO; BAR
echo foo; BAR
(6) using const makes the code easy to read, and const itself is a language structure, and define is a function
(7) const is much faster at compile time than define
Graphic:
Image resource Address: http://download.csdn.net/detail/zz249456649/8571357
Personally, talk to yourself.
Definition of a constant: in a page run or class, declare a value that is always constant
use: formulas, project profiles, site roots, and so on
The above describes the basic PHP constants, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.