Welcome to PHPMule.com
A blog site which I am posting to during my learning of the popular programming language PHP. Feel free to leave comments!
-
PHP - Conditional Statements
In PHP, you can use Conditional Statements to make decisions. Let’s say you wanted to give your customer a discount if he/she has an order over £5 (I know I’m generous!). You can easily do this by using the ‘If’ statement.
Let’s create the IF statement:
<?php
define(’CHOCOLATECAKE’, 3.50);
define(’SWISSROLL’, 1.50);$subtotal = CHOCOLATECAKE + SWISSROLL;
echo “Sub total of your order: £” . $subtotal . “. Thanks for visiting! “;
if( $subtotal >= 5.00 )
{
$totalcost = ($subtotal * 0.90);
$grandtotal = number_format($totalcost,2);
echo “This order qualified for a discount! “;
echo “Your grand total is: £” . $grandtotal;
}
else {
$subtotal = number_format($subtotal,2);
echo “Your grand total for your order is £” . $subtotal;
}
?>
As you may notice I am charging customer’s 3.50 for the cake and 1.50 for the swiss roll. If the customer bought both of these (equalling £5) it would be eligable for discount! The basic princpiple is that if the $subtotal is over the value of 5 then it creates a new variable called $totalcost. The $totalcost is worked out by the $subtotal being multiplied by 0.90 to subtract 10% off the order.
You will also notice there is an ‘Else’ function in there as well. If the ‘If’ function turns false (as in the order is below £5) the ‘Else’ function is performed. The Else funtion in this script will output the $subtotal variable if the order is below £5
The layout of writing an IF statement is as follows: if (your argument here) {commands here}
(Post created on Monday, April 6th 09 at 17:30)
-
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Jun | ||||||
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | |||
Archives
Recent Articles
- PHP - Rounding Off Numbers
- PHP - Single vs Double Quotation Marks
- PHP - Loading Files Into Your PHP Script By Using require() and include()
- PHP - Replacing Strings With Other Strings
- PHP - Finding Strings Within Strings
- PHP - Using strlen() To Test String Length
- PHP - Using The explode() Function
- PHP - Changing The String Case
- PHP - The nl2br() Function
- PHP - Trimming Strings
Links
Categories
- PHP (22)
- Uncategorized (1)


