define('PATH',"J:/php_files/");
echo constant('PATH'); // output is J:/php_files/
echo "<br>";
echo PATH; // output is J:/php_files/
We can get the value of a constant by using constant() function in PHP
constant(string);
If the constant does not exist then warning message will be generated while using this function
Define( 'Constant', Value);
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
define("SITE",'plus2net');
if(defined('SITE')){
echo "Constant SITE exist";
}else {
echo "Constant doesn't exist";}
In above code we have first declared a constant and then in 2nd line checked the existence so the if condition will return TRUE. By removing the first line we will get FALSE and else condition will execute.
Number of seconds in one minute
Interest Rate of Bank for the maturity calculation
We know the above values will not change during execution of the script. In some cases we have to use variables like this.
Time of adding the record to database table ( to prevent recursive posting within short interval by spammer )
Initial deposit amount in Bank
While using the constant name always keep them within quotes like defined('SITE')
To check the variables we use isset()
To check the functions we use function_exists()
To check the constants we use defined()
Declaring variablesGetting Type of Variable isset() empty()
Subscribe to our YouTube Channel here