Currently I have been behind a few projects. These projects are mostly build with php, mysql, python and a little javascript. Currently the projects I have been working on consist of the following.
Looping with an array
Recently while working on a search engine I needed to return what the user had searched for, separate the words into an array by the spaces then use a for loop to output the array content 1 by 1. The following code looks as follows.
$loader_keywords = $_POST['keyword'];
$keywords = explode(‘ ‘,$loader_keywords);
echo ‘<p>’;
for ($a = 0; $a < count($keywords); $a++) {
echo “$keywords[$a]” . ‘ ‘;
}
echo ‘</p>’;
Stepping Through The Code
$loader_keywords = $_POST['keyword'];
The first line (above) takes what the user has entered from the previous page and loads the content into a variable. Now we have something to work with.
$keywords = explode(‘ ‘,$loader_keywords);
The second line loads the variable $keywords from the explode function. This function takes the variable $loader_keywords and separates the content by the spaces. These spaces are stripped and the input words are loaded into an array. If the user searches for “happy bunny kittens” Then we will have an array that looks like this.
$keywords[1] = “happy”
$keywords[2] = “bunny”
$keywords[3] = “kittens”
The follow line has the single purpose of separating the array values into paragraphs.
echo ‘<p>’;
Now we are going to create a for loop to display the contents of the $keywords array.
for ($a = 0; $a < count($keywords); $a++) {
echo “$keywords[$a]” . ‘ ‘;
}
Now you can see that the we are having keywords outputted but the $a variable increased each time the loops is played through. You will also see that the loops limit is based on how how many keywords there are. This is counted using the very simple count function.
The rest of the code is used to close off the paragraph tags.
PHP – For loops
Basic Layout
The for loop is a powerful thing when learning PHP it is one of the fundamental prerequisites for creating conditions and accessing data from a database such as SQL. The basic ‘for loop’ looks as follows.
1 2 | for () { } |
Demo Loop
When the loop is loaded with content and a condition it will look something more like this:
1 2 3 | for ($i = 0; $i <= 9; $i++) { echo '<p>'.$i.'</p>'; } |
Code Breakdown
After beginning to break the code apart we will find that the first line works as follows:
1 2 3 4 | for ( //sets the beginning of the loop. $i = 0; //tells us that the variable is set from 0 even though this is the default. $i <= 9; //limits the loop so we don't have a string of numbers that never ends. $i++) //tells the variable i to increment by 1. |
When we run the code we will receive an output in our web browser that will look something like so:
1
2
3
4
5
6
7
8
9
This shows that the loop has worked through the conditions of ‘i.’ It simply sets $i to 0, adds 1, remembers the set variable, adds 1 again, remembers the new set variable and does so until the loops has reached its final condition which ends the loop. If you make a loops that does not end or you have a series of loops inside of each other that are very long, then you are going to have problems loading the web page. The most likely result will be a loading fail or a crash executing the script on the server.
Revise the Loop Structure
for (beginning variable; ending condition;increment)
{
Statements that will be executed for each time the loops runs.
}
Nesting the For Loop
It common for people to nest loops inside of loops to allow basic calculations, processes, or time. Say we wanted to create a multiplication table for 1′s that will only multiply up to 3 before changing to the next multiplication series.
1 2 3 4 5 6 | for ($firstnumber = 0; $firstnumber <= 3; $firstnumber++) { for ($secondnumber = 0; $secondnumber <= 3; $secondnumber++) { $calc = $firstnumber * $secondnumber; echo "$firstnumber x $secondnumber = $calc <br>"; } } |
Stepping Through the Code
Stepping through the code we recognize that the for loop begins and before closing the second for loop begins. Both of these loops will execute to give you a multiplication table; however, after the first loop runs through once the second loop will continue until done. When the second loop finishes it the loop reverts back to the parent and executes for the second round and thus the nested loop insides goes through 3 times again.
Results
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9


