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 - 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 - Using Loops With Arrays
There’s an even better way of displaying a numerical array’s contents by using a loop because the array is basically indexed by numbers.
We can use a ‘for’ loop to do this:
for ($counter = 0; $counter<10; $counter++) {
echo $celebrity_array[$counter] . ” “;
}
There’s another loop which you could use which was designed for arrays in the first place and that’s the ‘foreach’ loop:
foreach ($celebrity_array as $action_heroes) {
echo $action_heroes. ” “;
}
Here is a while loop that uses the each construct.
while ($variable = each($numbers)) {
echo $element['key'];
echo ” - “;
echo $element['value'];
echo “<br />”;
}
(Post created on Monday, April 13th 09 at 14:15)
-
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)


