What are the ways to define PHP constants?
In PHP, you can use define() and const. way to define constants.
But in development, when should we use define() to define constants, and when should we use const to define constants? What is the main difference between these two ways of defining constants?
Starting from version 5.3, PHP has two ways to define constants, using the const keyword or the define() method:
const FOO = 'BAR'; define('FOO', 'BAR');
The biggest difference between the two is that const is compiled Define constants at runtime, while the define() method defines constants at runtime.
const cannot be used in if statements, defne() can be used in if statements.
if(...) { const FOO = 'BAR';//错误 } if(...) { define('FOO', 'BAR');//正确 }
define() A common scenario is to first determine whether the constant has been defined before defining the constant:
if(defined('FOO)) { define('FOO', 'BAR') }
const When defining a constant, the value can only be a static scalar ( Number, string, true, false, null), and the define() method can use the value of any expression as the value of a constant. Starting from PHP 5.6, const also allows expressions to be used as the value of constants.
const BIT_5 = 1 << 5; //PHP5.6后支持,之前的PHP版本不支持 define('BIT_5', 1 << 5);// 所有PHP版本都支持
const only allows simple constant names, while define() can use the value of any expression as a constant name
for ($i = 0; $i < 32; $i++) { define('BIT_' . $i, 1 << $i); }
const The defined constant name is case-sensitive, and Case-insensitive constants can be defined by passing true to the third parameter of the define() method.
define('FOO', 'BAR', true); echo FOO; //BAR echo foo; //BAR
Listed above are some shortcomings or inflexibility of const compared to define(). Let’s take a look at why I personally recommend using const instead of define() to define constants (unless you need to Define constants in the scenarios listed above).
const has better readability, const is a language structure rather than a function, and is consistent with the form of defining class constants in a class.
const defines constants in the current namespace, and define() must pass the complete namespace name when defining to achieve similar effects:
namespace A\B\C; //To define the constant A\B\C\FOO: const FOO = 'BAR'; define('A\B\C\FOO', 'BAR');
const is available starting from PHP5.6 version Use arrays as constant values, and define() only supports using arrays as constant values starting from PHP 7.0.
const FOO = [1, 2, 3];// valid in PHP 5.6 define('FOO', [1, 2, 3]);// invalid in PHP 5.6, valid in PHP 7.0
Because const is a language structure and constants are defined at compile time, const will be slightly faster than define().
It is well known that PHP will affect efficiency after using define() to define a large number of constants. People invented apc_load_constants() and hidef to bypass the efficiency problems caused by define().
Finally, const can also be used to define constants in classes and interfaces, and define() can only be used to define constants in the global namespace:
class FOO { const BAR = 2;// 正确 } class Baz { define('QUX', 2)// 错误 }
Summary:
Unless you want to define a constant in an if branch or name a constant by the value of an expression, in other cases (even for simple code readability) it is recommended to use const instead of define( ).
Recommended: "PHP Tutorial"
The above is the detailed content of What are the ways to define php constants?. For more information, please follow other related articles on the PHP Chinese website!