Less Than or Equal To, Greater Than or Equal To

 

We can use the same code you created in the previous section to illustrate "Less Than or Equal To" and "Greater Than or Equal To". Change this line in your code:

$total_spent = 90;

to this:

$total_spent = 100;


Now run your code again. Did anything print?

The reason why nothing printed, and no errors occurred, is because we haven't written any condition logic to test for equality. We're only checking to see if the two variables are either Less Than ( < ) each other, or Greater Than ( > ) each other. We need to check if they are the same (as they now are).

Instead of adding yet another else if part, checking to see if the two totals are equal, we can use the operators <= (Less Than or Equal To) or >=(Greater Than or Equal To). Here's how. Change this line in your code:

else if($total_spent < $discount_total) {

to this:

else if($total_spent <= $discount_total) {

The only thing that's changed is the Less Than or Equal to symbol has been used instead of just the Less Than sign.

Now run your code again. Because we're now saying "If total spent is Less Than or equal to discount total, then execute the code." So the text gets printed to the screen.

 

Exercise
Suppose you want to apply the discount if 100 pounds or more has been spent. Change your code above to display the correct message. Use the >= symbol for this exercise.


Comparison Operators can take a little getting used, but are well worth the effort. If you're having a hard time with all these Operands, you'll be glad to hear that there's even more of them! Before we get to them, though, let's take a look at another logic technique you can use – the Switch Statement.