Home and Learn: PHP Programming Course

The Less Than ( < ) and Greater Than ( > ) symbols come in quite handy. They are really useful in loops (which we'll deal with in another section), and for testing numbers in general.

Suppose you wanted to test if someone has spent more than 100 pounds on your site. If they do, you want to give them a ten percent discount. The Less Than and Greater Than symbols can be used. Try this script. Open up your text editor, and type the following. Save your work, and try it out on your server.

<?PHP

$total_spent = 110;

$discount_total = 100;

if ($total_spent > $discount_total) {

print("10 percent discount applies to this order!");

}

?>

By using the Greater Than symbol ( **>** ), we're saying "If the
total spent is greater than the discount total then execute some code."

The Less Than symbol can be used in the same way. Change your script to this (new lines are in bold):

<?PHP

**$total_spent = 90;**

$discount_total = 100;

if ($total_spent > $discount_total) {

print("10 percent discount applies to this order!");

**}
else if($total_spent < $discount_total) {**

**print("Sorry – No discount!");**

**}**

**?>**

In the else if part added above, we're checking to see if the total spent is
Less Than ( **<**** **)100 pounds. If
it is, then a new message is display. Notice that the **$total_spent** variable
has been reduced to 90.

There is a problem with scripts such as the ones above, however. In the next
part, we'll take a look at the operators for **Less Than or Equal To** and
**Greater Than or Equal To**.

<-- Back One Page | Move on to the Next Part -->

Email us: enquiry at homeandlearn.co.uk