Home > Article > Backend Development > Detailed explanation of the difference between define and const in php
Everyone should know that define and const are constants in PHP, but how much do you know about them? This article mainly shares with you a detailed explanation of the difference between define and const in PHP, hoping to help everyone.
There is no dollar sign ($) in front of the constants;
Constants can only be used define() function definition, not through assignment statements;
Constants can be defined and accessed anywhere regardless of the scope of the variable;
Once defined, a constant cannot be redefined or undefined;
The value of a constant can only be a scalar.
Use of const | Use of define |
---|---|
const is used for the definition of class constants | Define cannot be used for the definition of class constants, but can be used for global constants. |
You cannot use const to define constants within functions, loops, and if statements. | define can use an ordinary constant name |
const | define can use an expression as the name |
const can only accept static scalars | define can use any expression |
const-defined constants are case-sensitive | define can use the first Three parameters (true indicates case insensitivity) to specify whether case sensitivity is present. |
Related recommendations:
Detailed examples of the difference between the usage of php constants define and const
The above is the detailed content of Detailed explanation of the difference between define and const in php. For more information, please follow other related articles on the PHP Chinese website!