Get latest news and Web Designing tutoria

Friday, 29 May 2015

PHP Less Than, Greater Than

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.

Related Posts:

  • Substraction in php We're not going to weigh things down by subjecting you to torrents of heavy Math! But you do need to know how to use the basic operators. First up is subtracting. To add up using PHP variables, you did this: <… Read More
  • More practice on Variables In the previous section, you saw what variables are: storage areas to hold things like numbers and text. You tell PHP to remember these values because you want to do something with them. In this section, you'll get… Read More
  • Adding up in PHP OK, let's do some adding up. To add up in PHP, the plus symbol (+) is used. (If you still have the code open from the previous page, try changing the full stop to a plus symbol. Run the code, and see what happens.)… Read More
  • Joining direct text and Variable You can join together direct text, and whatever is in your variable. The full stop (period or dot, to some) is used for this. Suppose you want to print out the following "My variable contains the value of 10". In PH… Read More
  • More variable practice In the previous section, you saw what variables are: storage areas to hold things like numbers and text. You tell PHP to remember these values because you want to do something with them. In this section, you'll g… Read More

0 comments:

Post a Comment