Constant
1. Constant composition = constant name + constant value
Constant name: [a- zA-Z0-9_] combination , and numbers cannot be used as the beginning, case-sensitive, uppercase is recommended
define: line of code, control structure, loop structure, function
The constants declared with define in the function can only be used after the function is called, otherwise an error will be reported as a character String output
(2) Scope
define and const Constants declared in the code line have global validity
const Constants declared in the class , can only be used in classes
(3) const uses an ordinary constant name, define can use an expression as the name
const FOO = 'BAR';
for ($i = 0; $i < 32; ++$i) {
define('BIT_' . $i, 1 << $i);
}
(4) const can only accept static scalars, while define can take any expression
For example :
_Const Bit_5 = 1 & LT; & LT; 5; // Invple Invalid
Define ('Bit_5', 1 & LT; & LT; 5); // Valid Valid
(5) Constants defined by const are case-sensitive, and define can specify whether case-sensitive is through the third parameter (true means case-insensitive)
For example:define('FOO', 'BAR', true);
echo FOO; // BAR
echo foo;
(6)
Using const makes the code simple and easy to read. const itself is a language structure, and define is a function
(7)
const is much faster than define during compilationIllustration:
Image resource address: http://download.csdn.net/detail/zz249456649/8571357
Just a personal comment
Definition of constants: When the page is running or in a class, declare a constant value
Usage:
Formulas, project configuration files, website root directories, etc.
The above has introduced the basic constants of PHP, including related content. I hope it will be helpful to friends who are interested in PHP tutorials.