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 - Single vs Double Quotation Marks
In PHP you can use either single quotation marks or double quotation marks. Anything contained between single quotation marks will be treated literally and anything between double quotation marks will be interpreted. For example:
echo ‘Here is a variable and the variable is $var’;
would literally output: Here is a variable and the variable is $var
If you put this line between double quotation marks it would output the following:
Here is a variable and the variable is 15.
There is one case where you may need to alternate the use the single and double quotes. Look at the following example:
echo ‘I will be going on holiday shortly and I’m taking my laptop with me’;
You will notice that you have used the ‘ symbol three times so therefore it will give a parse error. In this case you will have to use the double quotes. By the way this can happen vice versa as well so be careful when using single and double quotes in sentences.
(Post created on Friday, May 1st 09 at 23:35)
-
PHP - Finding Strings Within Strings
There are four functions you can use to find strings:
- strstr()
- strchr()
- strrchr()
- stristr()
The first function: strstr() can be used to find a string inside another or longer string for example:
if (strstr($string, ‘computer’))
echo “The word computer is found”;
This function is basically testing out whether or not the string ‘computer’ is in the variable $string. If the string is in variable $string it would output “The word computer is found” to the browser.
The second function we could use is strchr(). This function is the nearly similiar to the first function but this one finds a specific character such as ‘@’ or any other character.
The third function: strrchr() finds the last occurence of a character or string and then returns all the characters from that point onward. The last function is the stristr() function. This is the same as the strstr() function but it’s not case sensitive so you could find the string whether or not it’s in capitals or not (or both caps and not caps)
(Post created on Wednesday, April 15th 09 at 15:45)
-
PHP - Using strlen() To Test String Length
This is a very useful function if you wanted to validate your visitor’s input on a web form. You could use this function to test out the length of their email address. The mininum length of an email address is six characters.
To test out the string we would use an If function along with the strlen() to find out if the string is more than six characters:
if (strlen($emailaddress) < 6) {
echo ‘Your email address is not valid’;
exit;
}
We could use this function on other input fields if we wanted to and it’s one useful measure against your visitors from sending spam. You can also limit the size using this function as well:
if(strlen($forename) > 30 {
echo ‘Your forename is too long’;
exit;
}
It should be in your best interest to implement these features in your forms because of security. There are other functions as well which validate user data which I will go through in another blog post!
(Post created on Tuesday, April 14th 09 at 13:45)
-
PHP - Using The explode() Function
As it says the explode function is like sticking a dynamite on parts of the string to give you different sections of the string. Lets say we have a list of colors in a variable:
$colors = “blue;yellow;green;orange;white”;
What would happen if we explode them? They would be separated. It’s best if we make an array for them:
$colorarray = explode (”;”, $colors);
As we want to explode the ‘;’ we have to include this in the first part of the function as you can see in the above example. We then include the variable we want to explode after it. After the ‘explosion’ the ‘;’ is basically destroyed leaving the colors separated from each other and subsequently stored in the $colorarray.
(Post created on Monday, April 12th 09 at 22:10)
-
PHP - Changing The String Case
If you wanted to change the case of a string for example the word ‘television’ into ‘TELEVISION’ you can easily do so in PHP. You can do it vice versa as well.
Here are the following functions you can use to change the case of a string:
Let’s use the following functions with the variable ‘$television’ By the way the variable $television contains the value “what brand of tv do you own?”
strtoupper($television) = WHAT BRAND OF TV DO YOU OWN?
strrtolower($televison) = what brand of tv do you own?
ucfirst($television) = What brand of tv do you own?
ucwords($televison) = What Brand Of Tv Do You Own?
The first function converts the alphabetic characters to uppercase, the second to uppercase. The third function converts the first letter of the first word in uppercase and the fourth function converts the first letter of each word in uppercase.
These functions could be used in web forums and you would prefer your comments to start in uppercase. To be honest it really depends on how well you want to present your website I suppose.
(Post created on Monday, April 12th 09 at 19:30)
-
PHP - The nl2br() Function
This function stands for: New Line 2 Break
If the visitor wanted to see what he or she has entered on a form it would appear on one line if it was echoed. If we use the nl2br() function it would echo the feedback with the new lines replaced with breaks:
If we used it without this function is would look like this:
Forename: Nathan Comments: It looks better with the nl2br() dont you think?
If we used with nl2br() it would look like this:
Forname: Nathan
Comments: It looks better with the nl2br() dont you think?
echo nl2br($feedback);
(Post created on Monday, April 12th 09 at 17:15)
-
PHP - Trimming Strings
The trimming string functions:
In a string you can have excess whitespace you don’t need but is very useful if you want to store this string in a file or database.
The trim() function will remove any whitespace from the beginning or end of string for example if you had the string “ celebrity ” the resulting string will be “celebrity” as all the whitespace is removed from the start and end.
There are two other trimming functions and these are:
ltrim() and rtrim()
These functions do the same thing as the ‘trim()’ function but instead the first one removes whitespace from the left hand side and the other one removes whitespace from the right hand side.
The main purpose of trimming is to have cleaner input from your visitors, for example if they were to accidently include a space before they input their name on a form.
(Post created on Monday, April 13th 09 at 16:30)
-
PHP - Accessing And Replacing Array Contents
To access the contents of an array, you use it’s variable name along with its key (index) such as:
$celebrity_array[0]
so you can use it’s contents.
You can use array variables as you would with normal variables and you could easily change it’s contents by using the ‘=’ operator like the following example:
$celebrity_array[0] = ‘Daniel Craig’ ;
This line will change the contents of [0] to Daniel Craig. It was Bruce Willis by the way but now he’s gone…
(Post created on Friday, April 10th 09 at 23:35)
-
PHP - Using Arrays (Numerically Indexed)
An array is a variable that can store a set of values.
Numerically Indexed Arrays
If you wanted to store a set of values in one variable we could do that as follows:
$celebrity_array[0] = “Bruce Willis”;
$celebrity_array[1] = “Arnold Swarzennegar”;
$celebrity_array[2] = “Matt Damon”;
$celebrity_array[3] = “Liam Neeson”;
If we then wanted to reference to this we could use these lines:
echo “A well known action hero is ” . $celebrity_array[0] . ” and ” . $celebrity_array[3] . ” is pretty good as well! “;
You will also notice that the counter starts at ‘[0]‘ and it’s something you would have to get used to if you haven’t seen an array before.
Arrays obviously save time and it will make your code look a little tidier. I will go more into arrays in the next blog post as I’m still learning
(Post created on Thursday, April 9th 09 at 15:35)
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)


