logo
Blog single photo

PHP Constants


                        

Constant is an identifier for a simple value.

Syntax

 define(name, value, case-insensitive)  

Exapmle with case-sensitive or case-insensitive


case-sensitive

 <?php   
      define('myname','Dipankar',false);  
       echo myname;  
      ?>  


 <?php   
      define('myname','Dipankar',false);  
       echo Myname;  
 ?>  


case-insensitive

 <?php   
      define('myname','Dipankar',true);  
       echo Myname;  
 ?>  


PHP has large number of predefined constants.(Magic Constants)

  • __FILE__
  • __DIR__
  • __FUNCTION__
  • __CLASS__
  • __METHOD__
  • __LINE__
  • __NAMESPACE__

  • See Example


     <?php   
        class Mgc_class{   
           public function Mgc_public_function(){   
             echo "I am public function";   
           }   
           function Mgc_dir(){   
             echo "My Dir : '".__DIR__."'.<br>";   
             }   
           function Mge_file(){   
             echo "My File : '".__FILE__."'.<br>";   
           }   
           function Mgc_line(){   
             echo "Line : '".__LINE__."'.<br>";   
           }   
           function print_function_name(){   
             echo "Function Name : '".__FUNCTION__."'.<br>";   
           }   
           function print_class_name(){   
             echo "Class Name : '".__CLASS__."'.<br>";   
           }   
       }   
        $print = new Mgc_class();   
        $print-> Mgc_dir();   
        $print-> Mge_file();   
        $print-> Mgc_line();   
        $print-> print_function_name();   
        $print-> print_class_name();   
        ?>   
    

    Top