What is difference in defining the variables like
define('SITENAME', "My Site");
and
$sitename = "My Site";
which way is better to define configuration variables (such as site name/urls etc.)?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is the definition of a constant:
You cannot change the value, and you don’t refer to it with a
$prefix. In general, it has nothing at all to do with variables.This is a variable:
For values which are not expected to change during execution of your PHP script (e.g. some configuration parameters) it might be better to define constants. Also, constants have global scope (you cannot, and don’t need, to make them
global).On the other hand, keep in mind that you cannot iterate over a bunch of constants as you can iterate over an array of values. For example, you cannot pass a
$configarray and read multiple values from it without knowing which values you are interested in beforehand.Usually constants are used for options that may influence the behavior of the whole of your application, for example:
For other cases, variables are more convenient so they are commonly used even when technically their values are constant for the duration of your application’s run.