: This article mainly introduces constants in PHP. if you are interested in the PHP Tutorial, you can refer to them. Overview
The constant value cannot be changed during script execution.
Once defined, a constant cannot be redefined or undefined.
Constants are case sensitive by default. Traditionally, constant identifiers are always capitalized.
Use the define () function to define constants. after PHP 5.3.0, you can use the const keyword to define constants outside the class definition.
Constants cannot be defined in the function, in the loop, or in the if statement using const.
Constants can only contain scalar data (boolean, integer, float, and string ). Resource constants can be defined, but should be avoided as far as possible, because unexpected results may occur.
Unlike variables, the $ symbol cannot be added before constants.
Constant scope
Constants can be defined and accessed anywhere regardless of the scope of variables.
View the scope of a constant
Magic constant
The so-called magic constant is not a constant.
There are eight magic constants whose values change with their position in the code
Name |
Description |
_ LINE __ |
The current row number in the file. |
_ FILE __ |
The complete file path and file name. If it is used in a file to be included, the file name to be included is returned. Starting from PHP 4.0.2, __file _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution ), earlier versions sometimes contain a relative path. |
_ DIR __ |
The directory where the file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0) |
_ FUNCTION __ |
Function name (new PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters. |
_ CLASS __ |
Class name (new PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, the value is always lowercase letters. The class name includes the declared zone (for example, Foo \ Bar ). Note that _ CLASS _ from PHP 5.4 also applies to trait. When used in the trait method, __class _ is the name of the CLASS that calls the trait method. |
_ TRAIT __ |
Trait name (new in PHP 5.4.0 ). Starting from PHP 5.4, this constant returns the name (case sensitive) of the trait definition ). The Trait name includes the declared region (for example, Foo \ Bar ). |
_ METHOD __ |
Class method name (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ). |
_ NAMESPACE __ |
Name of the current namespace (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0 ). |
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces the constants in PHP, including the content, and hope to help those who are interested in the PHP Tutorial.