Php Constants

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.

Syntax:-


<?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;
?>
Output:- Removeload Academy is a free e-learning tutorial Portal

Try it Yourself

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;
?>
Output:- Removeload Academy is a free e-learning tutorial Portal

Try it Yourself

PHP Constant Array

In PHP7, you can create constant variable value into an array format.

Example:-


<?php
define('SOFTWARE_COMPANIES',array('TCS','HCL','Infosys','Oracle'));
echo SOFTWARE_COMPANIES[0]; //TCS
echo SOFTWARE_COMPANIES[1]; //HCL
echo SOFTWARE_COMPANIES[2]; //Infosys
echo SOFTWARE_COMPANIES[3]; //Oracle
?>

Try it Yourself