HOW TO USE CONSTANTS IN PHP
Constants are similar to variables in that they are containers that hold value. However, a constant’s value once set, cannot be changed in a script. Constants also do not have a $ in front like variables do. It is good programming practice to name constants in capitals, although PHP will also accept lowercase.
Use constants for a value that you know is set and won’t change in the script.
To declare a constant:
define("nameofconstant", "valueofconstant");
Example:
<?
define("TAX","0.10");
?>
This will declare a constant called TAX with a set value of 0.10.
Anywhere in your script, you an simply use the word TAX and PHP will know the value will be 0.10.
Example:
<?
$taxable=5000 * TAX; // equivalent to 5000 * 0.10
echo $taxable; // outputs 500
?>
|