Detailed explanation of the difference between PHP constants define and const
- 2021-12-11 07:08:09
- OfStack
Preface
A constant is a simple identifier. This value cannot be changed during script execution (except for so-called magic constants, which are not really constants). Constants are case sensitive by default. Constant identifiers are usually uppercase.
Constants can be defined using the define () function. After php 5.3. 0, you can use the const keyword to define constants outside the class definition, whereas the previous const keyword was only used in the class (class). Once a constant 1 is defined, it cannot be changed or undefined.
Constants can only contain scalar data (boolean, integer, float, and string). resource constants can be defined, but they should be avoided as far as possible, because they will cause unpredictable results.
The value of a constant can be obtained simply by specifying its name. Unlike variables, a constant should not be preceded by a $sign. If the constant name is dynamic, you can also use the function constant () to get the value of the constant. Use get_defined_contstants () to get a list of all defined constants.
Constants and variables differ as follows:
1. Constants are not preceded by a dollar sign ($)
2. Constants can only be defined by define () function, not by assignment statement
3. Constants can be defined and accessed anywhere regardless of the scope of variables
4. Constant 1 cannot be redefined or cancelled once it is defined
5. The value of a constant can only be a scalar
<?php
// The following code is in the PHP 5.3.0 After that, it can work normally
const USERNAME = 'zhouguowei';
echo USERNAME;
echo constant("USERNAME");
const ZHOUUSERNAME = 'zhouguowei2222222222';
define('MYUSERNAME','zhouguowei1111111');
echo "<pre>";
print_r(get_defined_constants());
?>
Q: What is the difference between const and define when defining constants in php?
Answer: Using const makes the code easy to read. const itself is a language structure, while define is a function. In addition, const compiles much faster than define.
1. const is used to define class member variables. 1 It is defined and cannot be modified. Define cannot be used to define class member variables, but can be used for global constants.
2. Const can be used in classes, but define cannot
3. Const can no longer define constants in conditional statements
<?php
if (...){
const FOO = 'BAR'; // Invalid invalid
}
if (...) {
define('FOO', 'BAR'); // Effective valid
}
?>
4. const uses a common constant name, while define can use an expression as its name
<?php
const FOO = 'BAR';
for ($i = 0; $i < 32; ++$i) {
define('BIT_' . $i, 1 << $i);
}
?>
5. const can only accept static scalars, while define can adopt any expression
<?php
const BIT_5 = 1 << 5; // Invalid invalid
define('BIT_5', 1 << 5); // Effective valid
?>
6. Constants defined by const are case sensitive, while define can specify case sensitive by the third parameter (true means case insensitive).
<?php
define('FOO', 'BAR', true);
echo FOO; // BAR
echo foo; // BAR
?>
Summarize