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 forgo any deliberation 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.
To manage precedence:
Open handle_calc.php in your text editor or IDE, if it is not already open (Script 4.3).
Replace the three lines that initially calculate the order total with the following (Script 4.4):
$total = (($price * $quantity) + $shipping) - $discount;
Script 4.4 By using parentheses, calculations made over multiple lines (compare with Script 4.3) can be condensed without affecting the script’s mathematical accuracy.
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>Product Cost Calculator</ title> 6 <style type="text/css"> 7 .number { font-weight: bold;} 8 </style> 9 </head> 10 <body> 11 <?php // Script 4.4 - handle_calc.php #3 12 /* This script takes values from calculator.html and performs 13 total cost and monthly payment calculations. */ 14 15 // Address error handling, if you want. 16 17 // Get the values from the $_POST array: 18 $price = $_POST['price']; 19 $quantity = $_POST['quantity']; 20 $discount = $_POST['discount']; 21 $tax = $_POST['tax']; 22 $shipping = $_POST['shipping']; 23 $payments = $_POST['payments']; 24 25 // Calculate the total: 26 $total = (($price * $quantity) + $shipping) - $discount; 27 28 // Determine the tax rate: 29 $taxrate = ($tax / 100) + 1; 30 31 // Factor in the tax rate: 32 $total = $total * $taxrate; 33 34 // Calculate the monthly payments: 35 $monthly = $total / $payments; 36 37 // Apply the proper formatting: 38 $total = number_format ($total, 2); 39 $monthly = number_format ($monthly, 2); 40 41 // Print out the results: 42 print "<p>You have selected to purchase:<br> 43 <span class=\"number\">$quantity</span> widget(s) at <br> 44 $<span class=\"number\">$price</span> price each plus a <br> 45 $<span class=\"number\">$shipping</span> shipping cost and a <br> 46 <span class=\"number\">$tax</span> percent tax rate.<br> 47 After your $<span class=\"number\">$discount</span> discount, the total cost is 48 $<span class=\"number\">$total</ span>.<br> 49 Divided over <span class=\"number\">$payments</span> monthly payments, that would be $<span class=\"number\">$monthly</span> each.</ p>"; 50 51 ?> 52 </body> 53 </html>
In this script, it’s fine 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.
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.
Save the script, place it in the same directory as calculator.html, and test it in your browser .