In this tutorial, you will be learning how to declare constants in PHP.
Declaring and Using Constants
So in the previous tutorial, you learned about variables.
Variable are stored data that once set can change their value.
Constants, on the other hand, are stored data that once set, the value cannot change.
All the previous concepts mentioned relating to datatype also applies to constants: constants can be all the types that variables can be.
For example, you could use the following syntax to create two constants.
Example
<?php
define("A", 100);
define("B", 200);
echo A;
echo "<br>";
echo B;
?>
Result
Proper Syntax
Please note that the names of the constants are in uppercase. You should write constants this way because it helps you remember whether something is a variable or a constant.
Another thing you should keep in mind is constants do not have a $
in front of them.
In summary, the two main traits of a constant name are: the letters are all capital and there is, unlike a variable, no $
at the front.