- 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
snippet
<?php
//create class
class rookienerd
{
//create constant variable
const a= "This is const keyword example";
}
//call constant variable.
echo rookienerd::a;
?>
Output:
Example 2
snippet
<?php
//create class
class demo
{
//create constant variable
const a= 10;
}
//call constant variable.
echo demo::a;
?>
Output: