Loop Holes
Last updated 24 Sep 2005
Thank you for using our NeoExtreme tutorials! We are pleased to know that we have helped you in some way today. If you have any questions or concerns about our tutorials, please contact us.
Tutorial
Now for what you came for!
Introduction:
An array is a data structure that allows you to hold data in a grid structure, similar to your multiplication tables. It is often necessary to loop through this structure and pull out the data. This can be used for creating tables in HTML, making reports or just outputting the results from a mySQL database.
Types of loops:
PHP has 4 types of loops with each behaving differently. The 4 types of loops are: while, do…while, for and foreach.
The While loop:
While loops will continue to loop while a condition is true. For example we might choose to loop through an array of colors until we find our favorite.
Syntax: while(condition)
Example:
As you can see from the example, we will continue looping through the colors until we get to "blue" my favorite color. Here is what we would see if we ran the code:
red is not my favorite color.The do..while loop:
yellow is not my favorite color.
green is not my favorite color.
blue is my favorite color!
do..While loops will continue to loop while a condition is met. However, unlike while this loop will always complete at least once, then continue until the conditionis false. For example we might choose to loop through an array of numbers until the total equals a certain max.
Syntax:
do {Example:
code
}
while(condition)
As you can see from the example, we will continue looping through the numbers until we get to the total of 11 because 11 is greater than our max of 10, so the loop stops.:
0 + 0 = 0The for loop:
0 + 1 = 1
1 + 4 = 5
5 + 2 = 7
7 + 4 = 11
The for loop is used when you know how many times you want to execute a statement or a list of statements.
Syntax:
for (initialization; condition; increment)Note: The for statement has three parameters. The first parameter is for initializing variables, the second parameter holds the condition, and the third parameter contains any increments required to implement the loop. If more than one variable is included in either the initialization or the increment section, then they should be separated by commas. The condition must evaluate to true or false.
{
code to be executed;
}
In our example we will loop through the list of numbers again, and add them all together.
Output:
0 + 0 = 0The foreach loop
0 + 1 = 1
1 + 4 = 5
5 + 2 = 7
7 + 4 = 11
11 + 5 = 16
This loop automatically loops through an entire array of values. While doing so it will assign the value of the array to the variable named. For example we will loop over an array of names and assign them to the $name variable.
Syntax:
foreach (array as value)Example:
{
code to be executed;
}
Output:
- Warrick
- Extremer
- Mr_Muffinman
- Valerie
- Koug
- B!
Loops are very handy for outputting data, especially when you use mySQL. For my last example I will show you how to output a user list.
Output:
- Warrick
- Extremer
- Mr_Muffinman
- Valerie
- Koug
- B!
Neoextreme is proud to be sponsored by Budgethost.ca. Check out Budgethost.ca to get the best deals for your website!
