Javatpoint Logo
Javatpoint Logo

Magic Constants

Magic constants are the predefined constants in PHP which get changed on the basis of their use. They start with double underscore (__) and ends with double underscore.

They are similar to other predefined constants but as they change their values with the context, they are called magic constants.

There are eight magical constants defined in the below table. They are case-insensitive.

Name Description
__LINE__ Represents current line number where it is used.
__FILE__ Represents full path and file name of the file. If it is used inside an include, name of included file is returned.
__DIR__ Represents full directory path of the file. Equivalent to dirname(__file__). It does not have a trailing slash unless it is a root directory. It also resolves symbolic link.
__FUNCTION__ Represents the function name where it is used. If it is used outside of any function, then it will return blank.
__CLASS__ Represents the class name where it is used. If it is used outside of any function, then it will return blank.
__TRAIT__ Represents the trait name where it is used. If it is used outside of any function, then it will return blank. It includes namespace it was declared in.
__METHOD__ Represents the name of the class method where it is used. The method name is returned as it was declared.
__NAMESPACE__ Represents the name of the current namespace.

Example

Let's see an example for each of the above magical constants.

File Name: magic.php

Output:

PHP Magic constants 1
Next TopicPHP Data Types




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA