PHP Magic Constant


 

PHP Magic Constant

In this tutorial we will study about magic constant, in PHP there are many magic constants are available,like _LINE_, _DIR_, _FILE_ etc. These constants are case-sensitive

In this tutorial we will study about magic constant, in PHP there are many magic constants are available,like _LINE_, _DIR_, _FILE_ etc. These constants are case-sensitive

PHP Magic Constants:

In PHP there are various predefined constants, but many of the constants are created by various extensions and could be implemented only when those extensions are available. These extensions could be available either by dynamic loading or they have been compiled before.

There are many magical constants are available in PHP, but seven magical constants are most important, like _LINE_, _DIR_, _FILE_ etc. These constants are case-sensitive. Use of the magic constants are as follows:

1) __LINE__: This constant is used to indicate the current line number of the file.

2) __DIR__ :  This constant is used to indicate the current directory in which the file is present

3) __FILE__ : This constant is used to indicate the full path of the file

4) __CLASS__ : Returns the class name

5) __FUNCTION__ : Returns the function name

6) __METHOD__ : Returns the function name

7) __NAMESPACE__ : Displays the namespace in which we are working.

 

Example:

<?php

function fun_co(){

echo __LINE__."<BR/>";

echo __FILE__."<BR/>";

echo __DIR__."<BR/>";

ECHO __FUNCTION__."<BR/>";

ECHO __METHOD__."<BR/>";

}

fun_co();

?>

Output:

3
C:\xampp\htdocs\PHP-AJAX\variable.php
C:\xampp\htdocs\PHP-AJAX
fun_co
fun_co

Ads