Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Magic Constants

PHP Predefined Constants

PHP has nine predefined constants that change value based on their context, which is why they are referred to as “magic constants.”

These magic constants are enclosed with double underscores at both the beginning and the end, except for the ClassName::class constant.

Magic Constants

Here are the magic constants, along with their descriptions and examples:

Constant

Description

 

__CLASS__

If used within a class, it returns the name of the class.

 

__DIR__

The file’s directory.

 

__FILE__

The complete path along with the file name.

 

__FUNCTION__

If used within a function, it returns the name of the function.

 

__LINE__

The line number currently being executed.

 

__METHOD__

If used within a function that belongs to a class, it returns both the class name and the function name.

 

__NAMESPACE__

If used within a namespace, it returns the name of the namespace.

 

__TRAIT__

If used within a trait, it returns the name of the trait.

 

ClassName::class

It provides the name of the specified class and, if applicable, the name of the namespace.

 

Note:

The magic constants are case-insensitive, so __LINE__ is equivalent to __line__.