Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the purpose of constant() function and differences between PHP constants and variables?

user-image
Question added by Mirza Adil Beg , Senior Web and Mobile App Developer , Confidential
Date Posted: 2017/04/12

Variables and Constant both carries some defined value but difference comes with their accessibility and value.

Constant can be define as

1. define('CONSTANT_NAME', 'constant_value'); OR

2. const CONSTANT_NAME = 'constant_value';

 

The second way is faster than the first one.

we can not change or over-write the value of constant throughout the application, therefore we can access the same value throughout the application. Doing so will cause fatal error.

 

Variable:

But in case of Variable, which can contain values in array, string, integer etc. format can be over-written by the function and their accessibility scope is very rigid sometimes only for a function or class or sub-classes. 

 

constant() function:

now Constant() function: is use to access the value of defined constant ex: constant('CONSTANT_NAME');will return value 'constant_value'; from the above example.

 

More Questions Like This