Specifications

(As you can see, PHP allows a lot of freedom in this areaall languages will let you change
the value of a variable, but not many will allow you to change the variables type, and even
fewer will let you change the variables name.)
The way these work is to use the value of one variable as the name of another. For example,
we could set
$varname = “tireqty”;
We can then use $$varname in place of $tireqty. For example, we can set the value of
$tireqty:
$$varname = 5;
This is exactly equivalent to
$tireqty = 5;
This might seem a little obscure, but well revisit its use later. Instead of having to list and use
each form variable separately, we can use a loop and a variable to process them all automati-
cally. Theres an example illustrating this in the section on for loops.
Constants
As you saw previously, we can change the value stored in a variable. We can also declare con-
stants. A constant stores a value such as a variable, but its value is set once and then cannot be
changed elsewhere in the script.
In our sample application, we might store the prices for each of the items on sale as constants.
You can define these constants using the define function:
define(“TIREPRICE”, 100);
define(“OILPRICE”, 10);
define(“SPARKPRICE”, 4);
Add these lines of code to your script.
You will notice that the names of the constants are all in uppercase. This is a convention bor-
rowed from C that makes it easy to distinguish between variables and constants at a glance.
This convention is not required but will make your code easier to read and maintain.
We now have three constants that can be used to calculate the total of the customers order.
Using PHP
P
ART I
24
03 7842 CH01 3/6/01 3:39 PM Page 24