Adding, Subtracting, Multiplying, and Dividing

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:

  1. Open your text editor and create a new document (Script 4.1).

     <HTML><HEAD><TITLE>Using Numbers  </TITLE></HEAD><BODY><?php?>  </BODY></HTML> 
    Script 4.1. While the calculations themselves are straightforward, you should feel free to add any other comments you feel necessary to illuminate the process here. If you want to improve your HTML skills, create a form that takes some information from the user (including Quantity and Discount) that gets passed to this script.

    graphics/04sc01.jpg

  2. Within the PHP tags, add

     $Cost = 2000.00; $Tax = 0.06;. 

    You have manually set the cost of the widget at $2,000.00. Note that you do not use either a dollar sign or a comma within the value of the variable. You have also manually set the sales tax rate at 6%, "0.06" being the proper way to write a percentage as a decimal. Both of these are floating-point numbers.

     $TotalCost = $Cost * $Quantity; 

    The asterisk (*) is used to indicate multiplication within PHP. The $Quantity value will be passed to the script as it would in a shopping cart application on a Web site. You can use the techniques demonstrated in Chapter 3, HTML Forms and PHP (see Inputting Data Manually), to make a form where the user enters a quantity, but here you'll just append that value to the URL.

     $Tax = $Tax + 1; // $Tax is now   worth 1.06. 

    Logically the plus sign (+) is used to perform addition. You can calculate how much something will cost with tax by adding 1 to the percent and then multiplying that new rate times the total. You've added a comment indicating as much for clarity sake (the comment can either be placed after the line, like I have here, on the next line, or omitted all together). One of the reasons I lumped together both types of numbers into one variable category is that, as you can see here, you can perform calculations on mixed variable types without any unfortunate consequences.

     $TotalCost = $TotalCost - $Discount; 

    To demonstrate subtraction using the minus (-) symbol, you'll assume that a discount may be applied, which will also be appended to the URL (or entered in a form).

     $TotalCost = $TotalCost * $Tax; 

    You can actually perform a calculation on a variable to determine its own value (in fact, it's quite common), but note that the original value of the variable is now gone. So on our line here, the original value of $TotalCost is replaced by the value of $TotalCost times $Tax.

     $Payments = $TotalCost / 12; 

    As an example of division, let's assume that the widget can be paid for over the course of one year. Hence, you have divided the total, including taxes and any applicable discount by 12 to find your monthly payment.

     // Print the results. 

    This comment differentiates where the calculations end and the output to the browser begins.

     print ("You requested to purchase   $Quantity widget(s) at $$Cost   each.\n<P>"); print ("The total with tax, minus   your $$Discount, comes to   $$TotalCost.\n<P>"); print ("You   may purchase the widget(s)in 12   monthly installments of }   $$Payments each.\n<P>"); 
  3. Save your script as numbers.php and upload it to your server.

  4. Test the script in your Web browser, insuring that you assign values to "Quantity" and "Discount" (Figure 4.1).

    Figure 4.1. Your working calculator is illustrated here. Make sure you append values for "Quantity" and "Discount" to the URL or create a form to pass those values along or you'll see results like those in Figure 4.2.

    graphics/04fig01.jpg

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.

graphics/04fig02.jpg

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


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net