PHP for the World Wide Web (Visual QuickStart Guide)
I l @ ve RuBoard |
Just as you learned in grade school, the most basic mathematics involve the principles of addition, subtraction, multiplication, and division. To demonstrate these principles, you'll create a PHP script that calculates the total cost for the sale of some widgets. This script could be the basis of a shopping cart applicationa very practical Web page feature. To create your sales-cost calculator:
You can experiment with these values (even omitting the variables completely as in Figure 4.2) to see how effectively your calculator works. Figure 4.2. Without receiving the necessary values, the script will still make its assigned calculations, resulting in errant data.
Tip As you will certainly notice, your calculator comes up with numbers that don't correspond well to real dollar values (see Figure 4.1). In the next section, Formatting Numbers, you'll learn how to adjust your numbers accordingly .
Tip If you wanted to print out the value of the total, before tax and before the discount (or both), there are two ways to do this. One would be to insert the appropriate print() statements immediately after the proper value has been determined but before the $TotalCost variable has been changed. The second method would be to use new variables to contain the values of the subsequent calculations (e.g., $TotalWithTax and $TotalLessDiscount).
Tip There are two methods you could have used to print a figure such as $2000.00. One is to escape the first dollar sign, as you did here; the other is to put a space between the dollar sign and the variable name , which would also create a space there in the browser. You cannot use $$Variable as the combination of two dollar signs together creates a type of variable too complex for the purposes of this book. |
I l @ ve RuBoard |