Loops
Loops are one of the most useful programming instructions it can allow you to do a task a number of times
or cycle through an array.
for loop
The first loop type we will talk about is the for loop. This is probably the commonest type of loop used
because it is a finite loop you define how long it should loop for at which point it will quit.
for($i=0; $i<3; $i++)
{
echo $i;
}
// echo's:
// 0
// 1
// 2
As you can see the for loop as three main parts, the
$i=0 initialises the counter variable. The
variable name can be anything for instance
$counter as long as the same name is used in all parts of the for loop.
The next part
$i<3 is the restriction on the amount of loops after each loop the for loop will only
carry on iterating if this expression is true, again the expression is up to you.
The final part
$i++ is executed at the end of each loop, in this case we are incrementing the value of
$i
by one although we could increase it by two, decrement it etc.
foreach loop
Another common loop that falls under the For loop category is the Foreach. This works in the same way except that instead of defining
how long it should loop for the loop iterates over an array, which once it has completed then stops iterating.
Here is the basic strucute...
foreach ($names as $value)
{
echo $value;
}
As you can see above the foreach loop is iterating over the array and returning the value of each element in the
$value
variable. However you can also iterate through the array and get each key and value of each array element.
foreach ($names as $key=>$value)
{
echo $key . ' - ' . $value ;
}
The
$key variable reads the key value of the array, while the
$value reads the $value part of the array.
while loop
The while loop is similar to the for loop. However this type of loop is a non-finite loop so can cause problems if the expression
never becomes false as it will continue to execute perhaps even causing PHP to hang.
$keepLooping = true;
while($keepLooping)
{
if ($person == "me")
{
$keepLooping = false
}
}
The expression
$keepLooping could be any type of expression, so
$i < 20 etc. As you can see the expression
is checked at the begininng of the loop so possibly the loop may never execute.
do-while loop
The do-while loop is very similar to the while loop however this type of loop will check to see if the iteration expression is true at the
end of a loop, therefore guaranteeing one loop is always executed.
$keepLooping = true;
do
{
if ($person == "me")
{
$keepLooping = false
}
}
while($keepLooping)
Again the expression
$keepLooping could be any type of expression, so
$i < 20 etc.
Looping control statements
These statements allow loops to be better controlled. Note that they apply to all the types of loop; for, foreach, while and do-while.
Break
This will break out of a loop, at the location in the code of the break statement. Code execution will then commence after the loop statement.
$keepLooping = true;
while($keepLooping)
{
if ($person == "me")
{
$keepLooping = false
}
if ($person == "sam")
{
break;
}
}
Therefore if the
$person variable is sam the while loop will stop executing.
You can also break out of more than one loop at once by putting a number after the break to indicate the number of loops you want to break out of.
for($i=0; $i<3; $i++)
{
$keepLooping = true;
while($keepLooping)
{
if ($person == "me")
{
$keepLooping = false
}
if ($person == "sam")
{
break 2;
}
}
}
This will break out of both the loops as there is a two after the
break
.
Continue
This statement allows the rest of the loop iteration to be skipped.
$keepLooping = true;
while($keepLooping)
{
if ($person == "sam")
{
continue;
}
if ($counter == 10)
{
$keepLooping = false
}
$counter++;
}
Therefore above when the
$person variable is sam the rest of that loop iteration is skipped, so the loop can't break for that iteration.
Finally you can skip more than one loop in a similar way to the break statement by putting a number after the
continue. The number represents
the number of loop iterations to skip.
$keepLooping = true;
while($keepLooping)
{
if ($person == "sam")
{
continue 5;
}
if ($counter == 10)
{
$keepLooping = false
}
$counter++;
}
As you can see above when the
$person is equal to sam five loop iterations are skipped.
No comments have been provided.
Written by Dominic Skinner
Last Updated: 2011-10-25 16:00:38