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__.

Căutare
Categorii
Citeste mai mult
Alte
PHP Add Array Items
Add Array Item To add items to an existing array, you can use the bracket [] syntax....
By PHP Tutorial 2024-05-17 08:06:05 0 3K
Alte
PHP Numbers
In this chapter we will look in depth into Integers, Floats, and Number Strings. PHP Numbers...
By PHP Tutorial 2024-05-17 07:37:05 0 2K
Alte
PHP Math
PHP has a set of math functions that allows you to perform mathematical tasks on numbers. PHP...
By PHP Tutorial 2024-05-17 07:39:09 0 2K
Alte
PHP while Loop
The while loop - Loops through a block of code as long as the specified condition is...
By PHP Tutorial 2024-05-17 07:48:58 0 2K
Alte
PHP Multidimensional Arrays
In the previous pages, we have described arrays that are a single list of key/value pairs....
By PHP Tutorial 2024-05-17 08:07:20 0 3K