WHILE loops
In PHP there are several different types of loops. Basically what a
loop does is evaluate a statement as true or false. If it is true it
executes some code and then alters the original statement and starts
all over again by re-evaluating it. It continues to loop through the
code like this until the statement becomes false.
Here is an example in its simplest form:
<?php
$num = 1;
while ( $num <=10 )
{
print $num . " ";
$num++;
}
?>
Basically what this does is: while a number is greater than or equal
to 10 it prints the number. The ++ adds one to the number, however
this could also be phrased as $num = $num + 1; Once the number becomes
greater than 10, in our case it becomes 11, then it stops executing
the code within the {brackets}
Below is an example of how you can combine a loop with a conditional
<?php
$num = 1;
while ( $num <=10 )
{
if ($num < 5)
{
print $num . " is less than 5 <br>";
}
else
{
print $num . " is not less than 5 <br>";
}
$num++;
}
?>
FOR Loops
A FOR loop is very similar to a WHILE loop in that it continues to
process a block of code until a statement becomes false, however
everything is defined in a single line. The basic structure for a FOR
loop is:
for ( start; conditional; increment) { code to execute; }
Let's go back to our first example using the WHILE loop, where we
printed out the numbers 1 through 10 and do the same thing using a FOR
loop.
<?php
for ($num=1; $num <= 10; $num++ )
{
print $num . " ";
}
?>
The FOR loop can also be used in conjunction with a conditional, just
like we did with the WHILE loop:
<?php
for ($num=1; $num <= 10; $num++ )
{
if ($num < 5)
{
print $num . " is less than 5 <br>";
}
else
{
print $num . " is not less than 5 <br>";
}
}
?>
FOREACH Loops
To understand FOREACH loops you have to remember what we learned about
arrays. If you recall an array (unlike a variable) contains a group of
data. When using a loop with an array, instead of having a counter
that goes until proven false the FOREACH loop continues until it has
used all values in the array. So for example if an array contained 5
pieces of data, then the FOREACH loop would execute 5 times. More uses
for arrays and FOREACH loops will become apparent when you start
importing data from MySQL.
A FOREACH loop is phrased like this: FOREACH (array as value) { what
to do; }
Here is an example of a FOREACH loop:
<?php
$a = array(1, 2, 3, 4, 5);
foreach ($a as $b)
{
print $b . " ";
}
?>
Once you understand that concept you can then use the FOREACH loop to
do more practical things. Let's say an array contains the ages of 5
family members. Then we will make a FOREACH loop that will determine
how much it costs for each of them to eat on a buffet that has varied
prices based on age. We will use the following pricing system: Under 5
is free, 5-12 years costs $4 and over 12 years is $6.
<?php
$t = 0;
$age = array(33, 35, 13, 8, 4);
foreach ($age as $a)
{
if ($a < 5)
{$p = 0;}
else
{
if ($a <12)
{$p = 4;}
else
{$p = 6;}
}
$t = $t + $p;
print "$" . $p . "<br>";
}
print "The total is: $" . $t;
?>