Web Statistics

Constants

The only thing more constant is change.

What is a constant?

As defined by www.php.net - "A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script... A constant is case-sensitive by default. By convention, constant identifiers are always uppercase. A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Defining a constant

A constant is defined by simply using the define() function. In the function parameters you will pass the constant name as the first argument and the value as the second. Only scalar data (boolean, integer, float and string) can be defined as a constants value.

				define("BASEBALL_PACKAGE", 12);
			

Constant Usage

The example above shows how define the constant BASEBALL_PACKAGE, and sets the value to (integer) 12 . To use this constant we would do something like the following:

				$balls = $packages * BASEBALL_PACKAGE;
			

Let's say that we know there are "always" (constantly) 12 balls in a package. All we need to do is plug the number of packages into the equation and we can get the total number of baseballs. Because the number of baseballs in the package never changes we can even reuse this constant to help determine how many more packages to order, the weight of a package based on the weight per ball, and even track the approximate inventory of baseballs and packages.

Up Next

Constant Magic