Const keyword

  • Constants are one type of variable which we can define for any class with keyword const.
  • Value of these variables cannot be changed any how after assigning.
  • Class constants are different than normal variables, as we do not need $ to declare the class constants.
  • If we are inside the class then values of the constants can be get using self keyword, but accessing the value outside the class you have to use Scope Resolution Operator.

Example 1

<?php  
    //create class  
    class javatpoint  
    {  
        //create constant variable  
        const a= "This is const keyword example";  
    }  
//call constant variable.  
echo javatpoint::a;  
?>  

Output:

This is const keyword example

Example 2

<?php  
    //create class  
    class demo  
    {  
        //create constant variable  
        const a= 10;  
    }  
//call constant variable.  
echo demo::a;  
?>  

Output:

10

Leave a comment