A constant in PHP. A constant is the identifier (name) of a simple value ). As its name implies, this value cannot be changed during script execution (except for so-called magic constants, they are actually not constants ). A constant is the identifier (name) of a simple value ). As its name implies, this value cannot be changed during script execution (except for so-called magic constants, they are actually not constants ). Constants are case sensitive by default. By convention, constant identifiers are always capitalized.
Constant names and any other PHP labels follow the same naming rules. A valid constant name starts with a letter or underline followed by any letter, number, or underline. The regular expression is like this: [a-zA-Z_x7f-xff] [a-zA-Z0-9_x7f-xff] *
Note:Here the letter is a-z, A-Z, and ASCII characters from 127 to 255 (0x7f-0xff.
Like superglobals, the constant range is global. You can access constants anywhere in the script without specifying the scope. For more information, see the variable range in the manual.
Syntax
You can use the define () function to define constants. Once a constant is defined, it cannot be changed or undefined.
Constants can only contain scalar data (boolean, integer, float, and string ).
You can simply get the value of a constant by specifying its name. do not add the $ symbol before the constant. If the constant name is dynamic, you can use the constant () function to read the constant value. Use get_defined_constants () to obtain a list of all defined constants.
Note: constants and (global) variables are in different namespaces. This means that for example, TRUE and $ TRUE are different.
If an undefined CONSTANT is used, PHP assumes that you want the name of the CONSTANT, just as you call it with a string (CONSTANT corresponds to "CONSTANT "). At this time, an E_NOTICE-level error will be issued. See why $ foo [bar] is incorrect in the manual (unless you define bar as a constant using define () in advance ). If you only want to check whether a constant is defined, use the defined () function.
Constants and variables are different:
There is no dollar sign before the constant ($ );
Constants can only be defined using the define () function, but cannot be defined using the value assignment statement;
Constants can be defined and accessed anywhere, regardless of the variable range rules;
Once defined, a constant cannot be redefined or canceled;
The constant value can only be a scalar.
Define constants
<?phpdefine("CONSTANT", "Hello world.");echo CONSTANT; // outputs "Hello world."echo Constant; // outputs "Constant" and issues a notice.?> |
Predefined Constants
PHP provides a large number of predefined constants to any script it runs. However, many constants are defined by different extension libraries. they only appear when these extension libraries are loaded, dynamically loaded, or included during compilation.
There are four magic constants that change based on their locations. For example, the value of _ LINE _ depends on the rows in the script. These special constants are case-insensitive, as follows:
Several PHP "magic constants"
The current row number in the _ LINE _ file.
The complete path and FILE name of The _ FILE.
_ FUNCTION name (new in PHP 4.3.0 ).
The name of The _ CLASS (new in PHP 4.3.0 ).
The METHOD name of The _ METHOD _ class (this is newly added in PHP 5.0.0 ).
Alias (name ). As its name implies, this value cannot be changed during script execution (except for so-called magic constants, they are not constants )....