Home > Article > Backend Development > What is the usage of const in php
const is a keyword in PHP, which is used to define constants. The syntax is "const constant name = constant value;"; when the const keyword defines a constant, it can only contain scalar data (bool, int, float, string). Once a constant is defined, it cannot be changed or undefined.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
const is a keyword in php, is Used to define constants.
In PHP, you can use the const keyword or the define() function to define a constant. The function define() allows a constant to be defined as an expression, while the const keyword has some restrictions. Once a constant is defined, it cannot be changed or undefined.
When using the const keyword to define a constant, it can only contain scalar data (bool, int, float, string). A constant can be defined as an expression or as an array. You can also define resource as a constant, but this should be avoided as it may cause unpredictable results.
Compared with variables, constants have the following characteristics:
There is no dollar sign ($) in front of the constant;
Constants can only be defined using define() and const;
The scope of constants is global;
Once defined, constants cannot be redefined or undefined.
The syntax of const keyword to define constants:
const 常量名 = 常量值;
Example:
<?php const url = 'https://www.php.cn/'; echo url; ?>
Recommended study:《PHP video tutorial》
The above is the detailed content of What is the usage of const in php. For more information, please follow other related articles on the PHP Chinese website!