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

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

Output:

Const keyword

Example 2

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

Output:

Const keyword
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +