ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل Mirza Adil Beg , Senior Web and Mobile App Developer , Confidential
تاريخ النشر: 2017/04/12
Anand Singh
من قبل Anand Singh

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.

 

المزيد من الأسئلة المماثلة