Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Mirza Adil Beg , Senior Web and Mobile App Developer , Confidential
Date de publication: 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