define()We can declare a constant by using define() function in PHP. Here is the syntax
Define( 'Constant', Value);
Let us start with some examples.
define("FNAME",'Alex');
echo FNAME; // output : Alex
echo Fname; // output : Fname
In the above code the 2nd output does not show the data actually stored as our declared constants are case sensitive by default. To make them case insensitive we have to use another optional parameter case_insensitive set to TRUE
define("FNAME",'Alex',True);
echo Fname; // output : Alex
We can't use hyphens while declaring any constants but we can use underscores
define("F-NAME",'Alex',True);
echo F-name; // output : 0
The difference between constant and variable is the data stored inside a constant is not changed throughout the execution of the program. Whereas variables can be assigned to different values at various stage of the program.
|