PHP approach to Magic Constants

In general Magic constant is the sum of the  series of numbers that are in Magic Squares. But in programming also it is possible to find Magic Constants as well.

However in PHP also magic constants are popular, in general they are like global predefined constants. The interesting fact here is, those constants are available due to execution of a script or scripts based on the place they have been called. Those are very useful and magically help and make the developers life easier.

If you are from C++ then this is not a new concept to you.

Following are magic constants and I tried to give an example to show how those constants are used in the script.

All the magic constants starts with __ (double underscores) and ends with __ (double underscores).


This gives the current line number where this magic constant is.

//__LINE__ magic constant
echo "The line number is ".__LINE__." .";

__LINE__ is more important in the DEBUG mode.


This returns the name of the function where the __FUNCTION__ is put inside.

echo "Show me the function I use\n";
echo showMyName();
function showMyName(){
return $functionaName;


This returns the file name of the file name including the full path.

echo "The file path :  ".__FILE__;

Above are the important magic constants that can be used frequently in your code. If you are into creative thinking you can find various ways to get the real benefit of those.


Please add your valuable idea below, will make a discussion, thanks !

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s