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)
-
PHP - Constants
A constant in PHP stores a value like a variable but if a Constant is given a value it can’t be changed in the script.
To declare a constant you have to set it out in the following way. Let’s say we wanted to give a chocolate cake a fixed price:
define(’CHOCOLATECAKE’, 1.50);
or even a swiss roll! ummm
define(’SWISSROLL’, 1.25);
If you wanted to use a constant all you have to do is use the name of the Constant such as:
echo CHOCOLATECAKE;
or even:
echo SWISSROLL;
Using these Constants will obviously output the price to the browser. You can use these constants to work out the total cost of both by using Variables and Constants together:
$totalcost = CHOCOLATECAKE + SWISSROLL;
The $totalcost now has a value of 2.75 and subsequently this variable is now of the ‘float’ data type as it’s a decimal number. You can see why PHP is a very good language to build customer order forms etc. You now have a variable that has a value and we can now output this to the browser:
echo “Total cost of your order: £” . $totalcost . “. Thanks for visiting!”;
As you can see you can effectively use constants and variables together!
(Post created on Monday, April 6th 09 at 14:15)
-
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)


