• No results found

Try It Out – Using a Combination of Operators

1.

Rev up your web page editor once more and type in the following: <HTML>

<HEAD></HEAD> <BODY>

<B>Namllu Car Insurance Quoter</B> <BR>

<BR>

<FORM METHOD=POST ACTION="quote.php"> What age are you?

<INPUT TYPE=TEXT NAME="Age" SIZE=3> <BR>

<BR>

What is the top speed of your car? <INPUT TYPE=TEXT NAME="Speed"> <BR>

<BR>

What is the approximate value of your car? <SELECT NAME="Value">

<OPTION VALUE=5000>Under $5,000</OPTION>

<OPTION VALUE=7000>Between $5,000 and $7,000</OPTION> <OPTION VALUE=10000>Between $7,000 and $10,000</OPTION> <OPTION VALUE=25000>Over $10,000</OPTION>

</SELECT> <BR> <BR>

What is the engine size of your car? <SELECT NAME="EngineSize"> <OPTION VALUE=1.0>1.OL</OPTION> <OPTION VALUE=1.3>1.3L</OPTION> <OPTION VALUE=1.5>1.5L</OPTION> <OPTION VALUE=2.0>2.0L</OPTION> </SELECT> <BR> <BR>

<INPUT TYPE=SUBMIT VALUE="Click here to Submit information for quote"> </FORM>

<HTML>

<HEAD></HEAD>

<BODY>

<B>Namllu Car Insurance Quoter</B> <?php

if ($Age<25 OR $Speed>100 OR $Value>10000 OR $EngineSize>1.5) {

echo ("We can offer you the $1500 Comprehensive package"); }

if ($Age>=65 AND ($Value<=5000 OR $Speed<=80) AND $Value<=10000 AND $EngineSize<=1.5 AND $Speed<=100)

{

echo ("We can offer you the $750 Senior Citizens Discount Package"); }

if (($Age<65 OR $Value>5000 AND $Speed>80) AND $Age>=25 AND $Speed<=100 AND $Value<=10000 AND $EngineSize<1.5)

{

echo ("We can offer you the $1000 Standard Cover Package"); }

?> </BODY> </HTML>

4.

Save this as quote.php.

6.

Click on the Submit button to see your quote:

How It Works

Yes, there are easier ways of doing this than the method used in this example, but we wanted to show you how to combine a large set of conditions in one statement. The first program, quote.html is straightforward but

lengthy HTML, which takes the four values from the user and stores them. In this example, we do make use of all the values the user has input. We're not going to examine the code in quote.html in any more detail,

other than to remark that it creates the (self-explanatory) variables $Age, $Speed, $EngineSize, and $Value in the HTML code, and passes their values to the server for use in our PHP script in our second

program.

What interests us more, are the complex conditions we now have to test for in quote.php. We have to

perform a total of three tests, and we have to make sure that every candidate who uses this insurance will come out with one quote and only one quote – Namllu aren't in the habit or turning people away without a quote! The first test is the one for comprehensive cover. There are four conditions we are testing for, and if any one of them is "true", then the user can only be recommended the most expensive insurance package. We test the

$Age variable to see if it's below 25, the $Speed variable to see if it's over 100, the $EngineSize variable

to see if it's over 1.5L, and the car's value to see if it's over $10,000. Then we display the details of the comprehensive package if a match is made.

if ($Age<25 OR $Speed>100 OR $Value>10000 OR $EngineSize>1.5) {

echo ("We can offer you the $1500 Comprehensive package"); }

Lastly, to trap anything else that might have come through, we check to see whether the $Age variable is

between the values of 25 and 64, the car's value is over 5000, and whether the top speed is over 80. This means that our driver can't qualify for the senior citizen's discount, but we also have to check that

he/she hasn't exceeded any of the other criteria set and therefore has to be offered a Comprehensive Package. So we check each of the $Speed, $Value, and $EngineSize variables.

if (($Age<65 OR $Value>5000 AND $Speed>80) AND $Age>=25 AND $Speed<=100 AND $Value<=10000 AND $EngineSize<1.5)

{

echo ("We can offer you the $1000 Standard Cover Package"); }

To sum up then, the conditions we are testing for in the above lines of code are:

❑ To qualify for the standard package, none of the expense conditions for age, value, speed or engine size can be broached.

❑ However, if you are a senior citizen, but don't meet the criteria for a senior citizen's discount (that is, you have a car that does in excess of 80 miles an hour and is worth more than $5000), then this also qualifies for the Standard coverage.

When you write conditions like this, you must make sure that they cover every eventuality by testing each possible outcome. In conditions like these, where working out in your head might not capture all situations, good design is the best way to work things out. Once you've worked it out on paper and implemented it, you should test it to make sure everything is caught.

This has been a rather complex example and you might be thinking, isn't there any easier way of doing any of this – are there any shortcuts? Well the good news is, there are some, and we'll look at some new features that will make programming PHP a bit easier.

Related documents