Breadcrumbs

PHP Loops

In all coding there comes a time when you have to execute a certain block of code a certain amount of times. Using loops you can do this and it is quite easy. PHP supports four loops, the while loop, do...while loop, for loop, and foreach loop. All are slightly different, but generically provide loops for various situations.

While Loop

While loops are useful and easy to use. The correct format for a while loop is:

Code: PHP

<?php
while (condition) {
    
Code to be executed;
}
?>

While loops will execute the specified the code block as long as the condition remains true. Conditional statements use the condition operators, to learn about operators and variables (both necessary for looping) use PHP Variables and Operators. Here is an example of a properly coded while loop.

Code: PHP

<?php
$i 
7;

while (
$i 1) {
    echo 
"I is greater than 1";
    
$i--;
}
?>

The code above declares a variable $i. Using a while loop it makes a conditional statement saying that as long as $i is less than 1 to execute the code block. The code block printing out the string will go 6 times.

The while loop is most used with MySQL (external link) to read the information from a database. An example of this being done would be a loop such as the one below:

Code: PHP

<?php
$sql 
"SELECT * FROM links"
$query mysql_query($sql); 

while(
$links mysql_fetch_array($query)) {
    echo 
'<a href="'.$links['url'].'" target="_blank">'.$links['name'].'</a><br />';
}
?>

A loop such as the one I previously posted uses a MySQL function that makes all the information collected from the SQL statement into an array for use in the code to be executed for each row of MySQL data.

Do...while Loop

The do...while loop is very similar to the while loop, but it always executes the code block at least once and further more as long as the condition remains true. The format for a do...while loop is:

Code: PHP

<?php
do
{
    
code to be executed;
}
while (
condition);
?>

The do...while loop you most likely won't use often, but in some situations it can be handy to know.

For Loop

The for loop is useful when you know the exact amount of times that you want to execute the loop. It combines the variable creation, conditional statement, and increment/decrement all into the declaration of the start of the for loop, technically these are the parameters.

Code: PHP

<?php
for (variableconditionincrement/decrement)
{
    
code to be executed;
}
?>

We can take the while loop from above and make it one easy for loop:

Code: PHP

<?php
for ($i 7; ($i >1$i--;)
{
    echo 
"I is greater than 1";
}
?>

For loops are useful, but yet the traditional while loop is easy to comprehend and is great for MySQL.

Foreach Loop

The foreach loop is specifically for looping through the values of an array. It loops over the array and each value for the current element of the array is stored in the specified variable and the array pointer is advanced by one to go the next element in the array. This loops you through the entire array.

Code: PHP

<?php
foreach (array as value)
{
    
code to be executed;
}
?>

The foreach loop can also be used for MySQL, but yet the while loop is much easier to use. An example of a foreach loop would be:

Code: PHP

<?php
$arr
=array("one""two""three");

foreach (
$arr as $value)
{
    echo 
"Value: " $value "<br />";
}
?>

Foreach loops are another useful loop that you will use when storing large amounts of data in arrays.

Summary

Most of the loops above you'll use sometime in your coding, and the most unlikely one that you will use is do...while loops. The rest are used often and make coding much easier. Now you can do the most useful task with loops going through MySQL databases allowing you to create powerful PHP applications with database interaction.

Tags: