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 - Rounding Off Numbers
I am back to learning php as I have been away for a while working on projects but hey I’m back!
Rounding numbers in PHP is very easy as I needed to include this function in one of my scripts…
Let’s say we had the following number: 5.76. Like most people we don’t need to see the extra digits as it makes the web page look more messy. You may want to round numbers because of many reasons and it’s a good function to use when rounding money etc.
If we wanted to round 5.76 to the nearest integer (6) we would need to do the following.
Let’s begin with variable $number containing 5.76
$number = 5.76;
Then we use the following expression:
$number = round ($number);
Now what will happen is that variable $number will now contain the rounded off number so when you echo or print it back to the browser you will see the number “6″ displayed. That’s all to it!
You can also round the number to a specified decimal place, for example:
$number = round ($number, 2);
will output 5.8 to the browser!
(Post created on Wednesday, June 10th 09 at 22:40)
-
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)
-
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)
-
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 | 31 | ||||
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)


