HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

Magic Constants in PHP



PHP Predefined Constants


PHP predefined constants are special variables with fixed values that are readily available throughout your code. These constants are defined by PHP itself or various extensions, and you can use them directly without needing to assign them values.

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




Here are the magic constants, with descriptions and examples:


 

Constant     Description

__CLASS__ If used inside a class, the class name is returned.

__DIR__ The directory of the file.

__FILE__ The file name including the full path.

__FUNCTION__ If inside a function, the function name is returned.

__LINE__ The current line number.

__METHOD__ If used inside a function that belongs to a class, both class and function name is returned.

__NAMESPACE__ If used inside a namespace, the name of the namespace is returned.

__TRAIT__ If used inside a trait, the trait name is returned.

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