13.09.2016 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Making Decisions with Conditionals<br />

49<br />

You can create code to calculate the discount using conditions <strong>and</strong> if <strong>and</strong> elseif statements.<br />

In this case, you need to use the AND operator (&&) to combine two conditions<br />

into one:<br />

if ($tireqty < 10) {<br />

$discount = 0;<br />

} elseif (($tireqty >= 10) && ($tireqty = 50) && ($tireqty = 100) {<br />

$discount = 15;<br />

}<br />

Note that you are free to type elseif or else if—versions with or without a space are<br />

both correct.<br />

If you are going to write a cascading set of elseif statements, you should be aware<br />

that only one of the blocks or statements will be executed. It did not matter in this<br />

example because all the conditions were mutually exclusive; only one can be true at a<br />

time. If you write conditions in a way that more than one could be true at the same<br />

time, only the block or statement following the first true condition will be executed.<br />

switch Statements<br />

The switch statement works in a similar way to the if statement, but it allows the condition<br />

to take more than two values. In an if statement, the condition can be either<br />

true or false. In a switch statement, the condition can take any number of different<br />

values, as long as it evaluates to a simple type (integer, string, or float).You need to provide<br />

a case statement to h<strong>and</strong>le each value you want to react to <strong>and</strong>, optionally, a default<br />

case to h<strong>and</strong>le any that you do not provide a specific case statement for.<br />

Bob wants to know what forms of advertising are working for him, so you can add a<br />

question to the order form. Insert this HTML into the order form, <strong>and</strong> the form will<br />

resemble Figure 1.6:<br />

<br />

How did you find Bob’s?<br />

<br />

I’m a regular customer<br />

TV advertising<br />

Phone directory<br />

Word of mouth<br />

<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!