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 - Loading Files Into Your PHP Script By Using require() and include()
If you wanted to reuse code from another file you could easily do this by using the require() or include() functions. These functions work exactly the same as server side includes files as in HTML.
These two functions work the same except when they fail to execute. If they fail the require() gives a fatal error and therefore stopping the execution of the remainder of the script and the include() function only gives a warning but doesn’t stop processing the script.
It’s recommended that you use the require() function rather than the include() function as your scripts shouldn’t be executing if for example your files are named wrong or placed in another directory.
Lets create a file called tester.php with the following file contents:
<?php
echo ‘I am a php script <br />’;
?>
Now in the index.php file we could have the following code:
<?php
echo ‘Another file will be included <br />’;
require ( ‘tester.php’ );
echo ‘Above is the contents of the tester.php file’;
?>
As you can see it’s quite simple to use other files in your php scripts and can save you a lot of time as you don’t need to reuse the code.
(Post created on Thursday, April 16th 09 at 13:50)
-
PHP - Replacing Strings With Other Strings
This is a very useful function to include in web forms or anywhere where user’s can leave input. What this function does is replace strings with strings for example, replacing swear words with something else such as ‘****’. This can be easily done in PHP by using the following format:
str_replace(findwhat, replacewith, whichstring)
If we used this format we could produce the following:
$modifiedcomments = str_replace(”water”, ‘fire’, $comments);
echo $modifiedcomments;This will replace water with fire in the variable $comments. So when the script get’s processed a new variable called $modifiedcomments will be created and if it finds the word or string ‘water’ it will be replaced with ‘fire’.
(Post created on Wednesday, April 15th 09 at 21:55)
-
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 - 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)
-
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)
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)


