PHP const: Define Constants Easily

In PHP, the const function is used to define constants. A constant is an identifier (name) whose value cannot be changed in the script. When defining a constant, the const keyword must be used with the following syntax:

const constant_name = value;

Constant names are typically written in capital letters and use underscores to separate words. The value of constants can be a scalar data type (e.g. integer, float, boolean, string) or an array.

Once a constant is defined, it can be used anywhere in the script without the need to use the $ sign to reference it. Once a constant is defined, its value cannot be changed throughout the entire script.

Here is an example of a constant:

const SITE_NAME = "My Website";
echo SITE_NAME; // 输出:My Website

Please note that constants defined with const are processed during the compilation phase, unlike variables defined with the define() function, therefore constants cannot be dynamically defined at runtime.

bannerAds