13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

42 Chapter 1 <strong>PHP</strong> Crash Course<br />

Figure 1.5<br />

The totals of the customer’s order have been calculated,<br />

formatted, <strong>and</strong> displayed.<br />

It also uses the number_format() function to format the totals as strings with two<br />

decimal places.This is a function from <strong>PHP</strong>’s Math library.<br />

If you look closely at the calculations, you might ask why the calculations were performed<br />

in the order they were. For example, consider this statement:<br />

$totalamount = $tireqty * TIREPRICE<br />

+ $oilqty * OILPRICE<br />

+ $sparkqty * SPARKPRICE;<br />

The total amount seems to be correct, but why were the multiplications performed<br />

before the additions? The answer lies in the precedence of the operators—that is, the<br />

order in which they are evaluated.<br />

Underst<strong>and</strong>ing Precedence <strong>and</strong> Associativity<br />

In general, operators have a set precedence, or order, in which they are evaluated.<br />

Operators also have an associativity, which is the order in which operators of the same<br />

precedence are evaluated.This order is generally left to right (called left for short), right<br />

to left (called right for short), or not relevant.<br />

Table 1.7 shows operator precedence <strong>and</strong> associativity in <strong>PHP</strong>. In this table, operators<br />

with the lowest precedence are at the top, <strong>and</strong> precedence increases as you go down the<br />

table.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!