PHP Magic Constants

0
2K

PHP Predefined Constants

PHP has nine predefined constants that change value depending on where they are used, and therefor they are called "magic constants".

These magic constants are written with a double underscore at the start and the end, except for the ClassName::class constant.


Magic Constants

Here are the magic constants, with descriptions and examples:

Constant Description  
__CLASS__ If used inside a class, the class name is returned. Try it »
__DIR__ The directory of the file. Try it »
__FILE__ The file name including the full path. Try it »
__FUNCTION__ If inside a function, the function name is returned. Try it »
__LINE__ The current line number. Try it »
__METHOD__ If used inside a function that belongs to a class, both class and function name is returned. Try it »
__NAMESPACE__ If used inside a namespace, the name of the namespace is returned. Try it »
__TRAIT__ If used inside a trait, the trait name is returned. Try it »
ClassName::class Returns the name of the specified class and the name of the namespace, if any. Try it »

Note:

The magic constants are case-insensitive, meaning __LINE__ returns the same as __line__.

Site içinde arama yapın
Kategoriler
Read More
Other
PHP Operators
PHP Operators Operators are used to perform operations on variables and values. PHP divides the...
By PHP Tutorial 2024-05-17 07:41:39 0 3K
Other
PHP - Modify Strings
PHP has a set of built-in functions that you can use to modify strings. Upper Case...
By PHP Tutorial 2024-05-17 07:29:19 0 3K
Other
PHP Data Types
PHP Data Types Variables can store data of different types, and different data types can do...
By PHP Tutorial 2024-05-17 07:27:24 0 2K
Other
PHP - Escape Characters
Escape Character To insert characters that are illegal in a string, use an escape character. An...
By PHP Tutorial 2024-05-17 07:32:36 0 3K
Other
PHP Shorthand if Statements
Short Hand If To write shorter code, you can write if statements on one line....
By PHP Tutorial 2024-05-17 07:47:03 0 2K