PHP Constant is a variable and you can use it anywhere in the code.
A constant variable doesn’t need a dollar($) sign before its name.
You can not alter a PHP constant once you have assigned its value.
You can create Php constant variable through the define() keyword.
How to create constant variable?
You can create a constant variable through the define() keyword. It has three parameters.
<?php define(NAME_OF_CONSTANT,VALUE,CASE-INSENSITIVE); ?>
NAME_OF_CONSTANT:- It represents the name of the constant variable. NAME_OF_CONSTANT that consists of letters and numbers. Constant variable name should be in upparcase format. It is mandatory.
VALUE:- It represents constant variable value. It is mandatory.
CASE-INSENSITIVE:- It represents case-sensitive false or true. By-default repesents false. It is optional.
Example:- Suppose, You create a constant variable REMOVELOAD_ACADEMY and put the value Removeload Academy is a free e-learning tutorial Portal after that call it through the constant variable name.
<?php define('REMOVELOAD_ACADEMY','Removeload Academy is a free e-learning tutorial Portal'); echo REMOVELOAD_ACADEMY; ?>
Example:- Now, you define the CASE-INSENSITIVE true.
<?php define('REMOVELOAD_ACADEMY','Removeload Academy is a free e-learning tutorial Portal',true); echo removeload_academy; ?>
PHP Constant Array
In PHP7, you can create constant variable value into an array format.
<?php define('SOFTWARE_COMPANIES',array('TCS','HCL','Infosys','Oracle')); echo SOFTWARE_COMPANIES; //TCS echo SOFTWARE_COMPANIES; //HCL echo SOFTWARE_COMPANIES; //Infosys echo SOFTWARE_COMPANIES; //Oracle ?>