Loops

A Loop is code that will run over and over again until something tells it to stop. Usually, a condition is tested, and if true, code will run. Then, the condition is tested again, and the code will keep running as long as the condition is true. Each run through the loop is called an 'iteration'.

A common danger with loops is the 'infinite loop'. Here, the condition tested will always be true, and the iterations never stop. This is a bad thing. Servers loop ad infinitum; resources are taxed to the max; the viewer's browser freezes; IT managers hang your picture on the dart board. The reverse is also possible; where a condition can never be true, but the programmer does not realize it. Here, the code never runs.

When running loops, we often have need to increase the value of a 'counter' during each iteration. For instance, if we want something to repeat ten times, we place a counter in the loop with an initial value of zero, then increase its value by 1 every iteration until the value equals ten. We test its value after every iteration as the condition precedent to running the loop again. In plain english, it appears thus:

    Set value of counter ($i = 0)
    Start loop
    Test condition (is $i less than 10?)
      if condition is true, continue with loop
      if condition is not true, end loop right here
    Run some code
    Increase the value of the counter ($i = $i+1)
    Start the loop again

As is true in most programming, the programmers get tired of typing, so we invent shortcuts. Consider the following:

$i=$i+1 is the same as $i+=1. Both increase $i by a value of 1.
You can substitute the number '1' with any number, so $i+=5 increases $i by a value of 5.
$i-=1 decreases $i by a value of 1. Again, you can substitute other numbers for '1'.
$i++ also increases $i by a value of 1. There is no substitution of numbers here.
$i-- decreases $i by a value of 1.

WHILE Loops

In plain english, a while loop reads: "While this condition is true, do this code". The syntax looks like so:

while (condition) { code to be performed }

Here are some examples:

<?php echo "0"; $i=1; while ($i < 10) { echo ", ".$i; $i++; } echo "<br />\n"; echo "5"; $xyz=10; while ($xyz < 51) { echo ", ".$xyz; $xyz+=5; } echo "<br />\n"; $day_of_week = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $i=0; while ($i < count($day_of_week)) { echo $day_of_week[$i]." "; $i++; } ?>

which displays thus:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9
5, 10, 15, 20, 25, 30, 35, 40, 45, 50
Sunday Monday Tuesday Wednesday Thursday Friday Saturday

What would happen if we did not include the '$i++;' code above? That would produce the infamous 'infinite loop'. Basically, $i would never increase within the loop. The condition ($i < 10) would always be true, and the computer would dutifully echo a series of '1's ad infinitum. It would still be running this time tomorrow.

What if we included it, but placed it outside the loop? Same result. The While loop would continue to print the number '1' until its condition read as false. Since the $i++ is outside the loop, $i never increases within the loop, and you become weary of seeing '1'.


FOR Loops

The for loop gives you an alternate method to control the number of iterations. In plain english, it reads "For this condition, do this code". The syntax looks like so:

for (set counter initial value; test condition; set increase for counter at end of loop)
{ code to be performed }

Here are some examples:

<?php for ($i=0; $i<10; $i++) { echo ", ".$i; } echo "<br />\n"; for ($i=0; $i<10; $i++) { if ($i==0) { echo $i; } else { echo ", ".$i; } } echo "<br />\n"; for ($i=0; $i < count($day_of_week); $i++) { echo $day_of_week[$i]." "; } ?>

which displays thus:

, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Sunday Monday Tuesday Wednesday Thursday Friday Saturday

Conditionals   < <  PREVIOUS   Table of Contents NEXT  > >   Functions

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2017, by Bill Sanders, all rights reserved.
This domain had 3,984 different visits in the last 30 days.
435,550 hits on this domain since 24 Nov 2006.
http://www.sandersongs.com/PHPsqlCourse/PHP06.php
This page was last modified on our server on 4 Jun 2008
and last refreshed on our server at 3:55 am, MST
This file took 0.00856 seconds to process.