PHP Magic constant



PHP Magic Constant

We already learnt about constant in our previous php tutorial. In this php post we will learn about some php ready made magic constants.
PHP script provides predefined magic constants.

PHP Magic constants :

__LINE__         Returns the current line number

__FILE__         Returns full path with filename of the file

__FUNCTION__ Returns the function name as it was declared.

__CLASS__      Returns the class name as it was declared.

__METHOD__   Returns the method name as it was declared.

PHP_VERSION   Returns the PHP version information

PHP __LINE__ constant Example

<?php

echo “Hello”;
echo ” Line No = ” . __LINE__;

?>

The output is :
Hello Line No = 4

PHP __FILE__ constant example

<?php

echo “Your File = ” . __FILE__;

?>

The output is:
Your File = C:\wamp\www\meera.php

PHP _FUNCTION__ constant example

<?php

function demo()
{

echo “The function name = ” . __FUNCTION__;

}

demo();
?>

The output is :
The function name = demo

PHP __METHOD__ and _CLASS constants Example

<?php

class test
{

function demo()
{
echo “The class name = ” . __CLASS__ .”<br/>”;
echo “The method name = ” . __METHOD__;

}
}

$obj =new test();
$obj->demo();

?>

The output is:
The class name = test
The method name = test::demo

PHP_VERSION Example

<?php

echo “The PHP version = ” . PHP_VERSION

?>

The output is :
The PHP version = 5.5.12

Leave a Reply

Your email address will not be published. Required fields are marked *