Creating CONSTANTS with define()

Chapter 5 #105 - Martha's CIS 52

ASSIGNMENT/EXPLANATION

"Variables offer a flexible way of storing data because you can change their values and the type of data they store at any time during the execution of your scripts." It's no surprise that constants offer the opposite advantage - they do NOT change, unless they are deliberately changed through the same process that created them. Programmers can create their own customized constants using PHP's built-in define() function. The value defined can be a number, a string, or a Boolean. The syntax takes the form of: define("YOUR_CONSTANT_NAME", 42). Inside the parentheses is the name of the constant, followed by a comma, followed by the value specified for the constant.

PHP also provides plenty of built-in constants, such as:

Exercise

It is the year 2009

CODE


define("THE_YEAR", "2009");
echo "It is the year " .THE_YEAR;