PHP provides a large number of predefined constants to any script it runs. However, many constants are defined by different extensions, and are only available when the extensions are loaded, either dynamically or at compile time.
There are seven magic constants whose values change as they position in the code. For example, the value of __line__ depends on the line in which it is located in the script. These special constants are case-insensitive, as follows:
name |
Description |
__line__ |
The current line number in the file. |
__file__ |
The full path and file name of the file. If used in the included file, the included file name is returned. Since PHP 4.0.2,__file__ always contains an absolute path (if the symbolic connection is a parsed absolute path), and the previous version sometimes contains a relative path. |
__dir__ |
The directory in which the file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to dirname (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0) = |
__function__ |
Function name (PHP 4.3.0 new). From PHP 5 This constant returns the name (case-sensitive) of the function when it is defined. This value is always lowercase in PHP 4. |
__class__ |
The name of the class (PHP 4.3.0 new). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). This value is always lowercase in PHP 4. |
__method__ |
The method name of the class (PHP 5.0.0 new). Returns the name (case-sensitive) of the method when it is defined. |
__namespace__ |
The name of the current namespace (case sensitive). This constant is defined at compile time (PHP 5.3.0 new) |