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 - Variable Types
In PHP, there are 6 data types:
Some of them everybody may know of already:
String - This data type has quite simply a string of data such as flower231tulip
Integer - This data type holds numbers such as 834
Float - This data type is used for real numbers such as 12.45
Boolean - This data type is used for true or false values
Array - Stores multiple data items
and the Object data type.
If a variable hasn’t got a value the data type is known as ‘NULL‘ to indicate nothing basically!
(Post created on Monday, April 6th 09 at 13:25)
-
PHP Variables
In PHP, variables are used for storing a value, whether it’s a text string or numbers.
A variable in PHP has to start with the ‘$’ sign followed by whatever you want to call the variable and then the variables value which would look something like this:
$dvd = “Casino Royale”
a number can also be stored in a variable:
$ageoffilm = 3
So what would happen if we made a PHP script using these variables?
<?php
$dvd = “Casino Royale”;
$ageoffilm = 3;
?>
<html>
<head><title>Movies</title></head>
<body>
<?php
echo “Best bond film: ” . $dvd . “<br />”;
echo “How old is the film: ” . $ageoffilm . “<br />”;
?>
</body>
</html>
This would output:
Best bond film: Casino Royale
How old is the film: 3By the way you may notice the cotcatination operator which is basically just a period “.”
You can use this operator to add strings together as shown in the example above.
(Post created on Sunday, April 5th 09 at 23:45)
-
PHP - Dynamic Content
Dynamic content in PHP basically means that the content changes. For example: If a visitor to your site submitted a comment you could have your script to output the date and time of the comment back to the user. As the date and time will be different in every comment it’s known as dynamic content. Let’s use the date function:
<?php
echo “<p>Comment processed. Date and time processed: “;
echo date (’H:i, jS F Y’);
echo “</p>”;
?>
The above will output to the browser: Comment processed. Date and time processed: 18:20, 4th April 2009
Quite easy to be honest and PHP can be used in many ways like this!
(Post created on Saturday, April 4th 09 at 18:40)
-
PHP Comments (using single-line and multi-line comments)
To make our lives easier we can use comments to let yourselves or other programmers know what a piece of code is doing. These are very much like notes as it describes what something is doing. This is essential in PHP when you look back at the code.
The PHP interpreter will ignore any comments as long as you put these comments in the correct format. There are two types of comments. One called single-line comments and one called multi-line comments which are both self explanatory really!
To insert a single line comment all you have to do is insert the ‘//’ after a line of code. For example:
echo ‘<p>Hello world.</p>’; // This is a statement that sends to the browser Hello world.
To insert a multi-line comment you have to insert your comment between these two tags: /* and */ anywhere in your code (most probably at the start or end of your script). For example:
/* Created by: Nathan
Last modified: 4th April 2009
Script purpose: Process comments
*/
Inserting comments is something you should get aquainted to as it can really help finding out what bits of code does what.
(Post created on Saturday, April 4th 09 at 17:50)
-
Launching the PHPMule.com blog
Hiya, I’m very new to the PHP world of programming and I have decided to create this blog for my own personal use so I can easily log what I do. I do welcome others to see what I blog and by all means feel free to leave any comments if you wish.
Nathan
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)


