Constant is important variable in any technology. When you are creating any application based on any Technology, You need to define some constant variable which will not change in throughout application.
A constant Stands for “A constant is an identifier (name) for a simple value”.
Main difference between constant and Variable
The main difference between a constant and a variable is variable may be changed but constant cannot be change in application.
Main feature of Constant
1- Constant cannot be change.
2- Define in single time and used multiple time based on requirement.
3- By default case sensitive.
4- Constant name will be in capital letter.
Here is a basic example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php define("URL", "http://phpflow.com"); define("MESSAGE", "Hello Parvez"); echo URL //output will http://phpflow.com echo MESSAGE // output Hello Parvez echo URL." is developed by ".MESSAGE // outputs http://phpflow.com is developed by Parvez ?> [/php] So at the time of using constant must be remember that constants are case sensitive, so below wouldn’t work [code type="php"] <?php echo URL; ?>
To solve case sensitive constant problem in php,PHP define() function has third parameter to control case problem.
To make it case-insensitive, all you need to do is set that third parameter to true.
1 2 3 4 5 6
<?php define("URL", "http://phpflow.com", true); echo SITE // outputs http://phpflow.com echo Site // outputs http://phpflow.com ?>