PHP: Using constants
We will be learning how to use constants.
The difference between a constant and variable, is a constant can only be defined once, i.e. its value can only be set one time, while a variable can have its value changed many times throughout the running of the program.
One reason to use constants is to set values that you know will should not change during the running of the program, such as interest rates, or other values which should not change while the PHP program is running.
In this case, I create a constant called AGE, and set a value of 18 to it, by using the following code:
define(”AGE”,18);
You can then echo or output in browser the value of the constant called AGE, using the following code:
echo AGE;
Note: you cannot define the same constant more than one time, during the course of the program running, otherwise notice message will be given, informing you that the constant has already been defined. This means that a constant value once set cannot be changed.

defining the same constant more than one time

defining the same constant more than one time causes notice message to be displayed
Note: the value of AGE constant will still be 18, even though the second define statement tries to change it to 21, because a constant can ONLY be defined once.

Comments »
No comments yet.
RSS feed for comments on this post.