Publishers of technology books, eBooks, and videos for creative people

# PHP for the Web: Visual QuickStart Guide: Using Numbers

• Print
This chapter is from the book

## Understanding Precedence

Inevitably, after a discussion of the various sorts of mathematical operators comes the discussion of precedence. Precedence refers to the order in which a series of calculations are executed. For example, what is the value of the following variable?

`\$number = 10 − 4 / 2;`

Is \$number worth 3 (10 minus 4 equals 6, divided by 2 equals 3) or 8 (4 divided by 2 equals 2, subtracted from 10 equals 8)? The answer here is 8, because division takes precedence over subtraction.

Appendix B, “Resources and Next Steps,” shows the complete list of operator precedence for PHP (including operators that haven’t been covered yet). However, instead of attempting to memorize a large table of peculiar characters, you can bypass the whole concept by using parentheses. Parentheses always take precedence over any other operator. Thus:

```\$number = (10 − 4) / 2; // 3
\$number = 10 − (4 / 2); // 8```

Using parentheses in your calculations ensures that you never see peculiar results due to precedence issues. Parentheses can also be used to rewrite complex calculations in fewer lines of code. Let’s rewrite the handle_calc.php script, combining multiple lines into one by using parentheses, while maintaining accuracy.

#### Script 4.4. By using parentheses, calculations made over multiple lines (see Script 4.3) can be condensed without affecting the script’s mathematical accuracy.

```1    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5       <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6       <title>Product Cost Calculator</title>
7       <style type="text/css" media="screen">
8          .number { font-weight: bold;}
9       </style>
11   <body>
12   <?php // Script 4.4 - handle_calc.php #3
13   /* This script takes values from calculator.html and performs
14   total cost and monthly payment calculations. */
15
16   // Address error handling, if you want.
17
18   // Get the values from the \$_POST array:
19   \$price = \$_POST['price'];
20   \$quantity = \$_POST['quantity'];
21   \$discount = \$_POST['discount'];
22   \$tax = \$_POST['tax'];
23   \$shipping = \$_POST['shipping'];
24   \$payments = \$_POST['payments'];
25
26   // Calculate the total:
`27   \$total = ((\$price * \$quantity) + \$shipping) − \$discount;`
28
29   // Determine the tax rate:
`30   \$taxrate = (\$tax/100) + 1;`
31
32   // Factor in the tax rate:
33   \$total = \$total * \$taxrate;
34
35   // Calculate the monthly payments:
36   \$monthly = \$total / \$payments;
37
38   // Apply the proper formatting:
39   \$total = number_format (\$total, 2);
40   \$monthly = number_format (\$monthly, 2);
41
42   // Print out the results:
43   print "<p>You have selected to purchase:<br />
44   <span class=\"number\">\$quantity</span> widget(s) at <br />
45   \$<span class=\"number\">\$price</span> price each plus a <br />
46   \$<span class=\"number\">\$shipping</span> shipping cost and a <br />
47   <span class=\"number\">\$tax</span> percent tax rate.<br />
48   After your \$<span class=\"number\">\$discount</span> discount, the total cost is
49   \$<span class=\"number\">\$total</span>.<br />
50   Divided over <span class=\"number\">\$payments</span> monthly payments, that would be \$<span class=\"number\">\$monthly</span> each.</p>";
51
52   ?>
53   </body>
54   </html>```

To manage precedence:

1. Open handle_calc.php in your text editor or IDE, if it is not already open (Script 4.3).
2. Replace the three lines that initially calculate the order total with the following (Script 4.4):
`\$total = ((\$price * \$quantity) + \$shipping) − \$discount;`

There’s no reason not to make all the calculations in one step, as long as you use parentheses to ensure that the math works properly. The other option is to memorize PHP’s rules of precedence for multiple operators, but using parentheses is a lot easier.

3. Change the two lines that calculate and add in the tax to this:
`\$taxrate = (\$tax/100) + 1;`

Again, the tax calculations can be made in one line instead of two separate ones.

4. Save the script, place it in the same directory as calculator.html, and test it in your browser and .