Constant
A constant is the identifier (name) of a simple value ). As its name implies, during script executionValue cannot be changed(Except for so-called magic constants, they are not constants ). Constant isCase Sensitive.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 expressed as follows:[A-Za-Z _ \ x7f-\ xFF] [a-zA-Z0-9 _ \ x7f-\ xFF] *
Example #5 Valid and invalid constant names
<? PHP
// Valid constant name
Define ( " Foo " , " Something " );
Define ( " Foo2 " , " Something else " );
Define ( " Foo_BAR " , " Something more " );
// Invalid constant name
Define ( " 2foo " , " Something " );
// The following definition is valid, but it should be avoided: (do not start with _ as a custom constant)
// Maybe one day, PHP will define a magic constant of _ Foo _.
// This will match yourCodeConflict
Define ( " _ Foo __ " , " Something " );
?>
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.
Define defines global constants that can be accessed anywhere.
Const is a global variable that defines the class and cannot change its value.
<?PHP
ClassSay_const {
ConstCharset="China ";
Publice function say_hello (){
Echo slef: charset;
}
}
$ Const1=NewSay_const ()'
$ Const1->Say_hello ();
?>
For more information, see the following extension:
- Additional reading:
- 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;
- Constants can only contain scalar data (Boolean, integer, float, and string). Do not define resource constants.
- You can use the constant () function to read the value of a constant. get_defined_constants () to obtain a list of all defined constants.
- If an undefined constant is used, PHP assumes that it wants the name of the constant, just like calling it with a string (constant corresponds to "constant "), an e_notice error is reported.
- PHP's "Magic constant ".
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------
Magic constant
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 seven magic constants whose values change with their positions in the code. For example_ Line __The value depends on the rows in the script. These special constants are case-insensitive, as follows:
Several PHP "Magic constants"
| 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. Since PHP 4.0.2,_ File __It always contains an absolute path (if it is a symbolic link, it is the absolute path after resolution), and the previous version sometimes contains 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 equivalentDirname (_ 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. |
| _ 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) |
<? PHP
Echo _ file __;
Echo _ class __;
Echo _ file __;
?>
SeeGet_class (),Get_object_vars (),File_exists ()AndFunction_exists ().