|
In lesson 2, we have
learned about one of the most common loops being used in the programming
world, the for loop. Now, there are 4 more types of loops other than the
for
loop. One is the foreach
loop. This is similar to the for
loop but it was made specially to iterate over arrays. The next type of
loop is the do..while
loop. This construct executes the statements within its scope for one
to a defined number of times. The third type is called the while
construct. This construct is similar to the do..while
construct except that in the while
loop, the statements in its scope can be bypassed. The fourth one is more
advanced and will not be discussed further later on. It is called recursive
procedures/functions. This is not actually a loop but a function or
procedure which calls itself, thus simulating a loop. Recursive procedures/functions
are used by more complex programs in almost all programming languages.
FOREACH
As stated above, foreach
is used to iterate over arrays. This is very similar to the same control
structure in C++. The basic syntax for a foreach
statements are:
foreach (array_expression as $value) statement;
and
foreach (array_expression as $key => $value) statement;
When a foreach
loop is used, the array pointer being used for the loop is automatically
repositioned to the first index position in the array. It would also be
worthwhile to consider that foreach
operates on a copy of the array, not the array itself. If the first syntax
is used, it would take in the value of the array and store it in $value
upon its initial pass through the loop. It is then up to the programmer
to decide on how to use the value in $value because in the next iteration,
the $value would change to the value of the next item in the array. Here
is an example:
// This loop will traverse through the array $MyArray and display
// the value of each element in the array.
$MyArray = array ("PHP", "MySQL", "Apache");
foreach ($MyArray as $value)
{
echo "Value: $value \n"; // this will print "Value: PHP" on its first iteration
}
The second syntax is almost the same with the first
except that the current element's key is stored in the variable $key.
For example:
// This loop will traverse through the array $MyArray and display
// the value of each key-value pair in the array.
$MyArray = array ("Webserver" => "Apache",
"Language" => "PHP",
"Database" => "MySQL");
foreach ($MyArray as $key => $value)
{
echo "Key: $key <-> Value: $value"; // this will print:
// "Key: Webserver <-> Value: Apache"
// on its first iteration
}
DO..WHILE
In do..while
loops, truth expressions are checked at the end of each iteration. This
guarantees the loop to execute at least once. The syntax for a do..while
loop is:
do
{
..statements..
} while (expr);
This is a fairly simple construct. Look at the
example below to fully get a grasp on do..while.
$x = 1; // an initial value should be present for the variable that
// will be incremented
do
{
$y = $x + 1; // let's make $y always have a value of one greater than $x
$x++; // if this is not incremented, this will be an endless loop
echo "$x : $y"; // display values for $x and $y
} while ($x <= 3); // this expression sets the loop to execute continiously
// until $x reaches 3
WHILE
The only difference this construct has with do..while
is the possibility of bypassing the loop if the loop condition is not
met. This is because truth expressions are checked even before an iteration
takes place. The basic syntax is:
while (expr) statement;
As you can see, while
loop is very simple. In fact, this is the simplest type of loop in PHP.
An example of this loop in action is:
$i = 1; // initialize $i
while ($i <= 3) // set expression where truth will be checked
{
echo "Value = $i"; // display current value
$i++; // increment $i to prevent endless loop
}
There! This ends our section on PHP loops. Remember
that loops can be a powerful ally but it can also be a destructive enemy
if used in the wrong sense. The general rule on loops is: "Avoid
endless loops". As we have seen in the examples above, a possible
way to get into this dilema is forgetting to increment (or decrement)
a variable which happens to be the control variable for the loop. With
your knowledge on loops, you can now create shorter codes and traverse
through database tables more effectively. Of course that would be tackled
on later lessons.
|