Just Can't GET Enough

This script outputs times tables if it receives an appropriate query string via the URL. The format for a for loop is shown here.

<title>PHP Test</title>
if (isset($_GET['times']))
   $thetables= $_GET['times'];
   echo "<ul> \n";
   for ($i=1;$i<=12;$i++)
      $result = $thetables * $i;
      echo "<li>".$i." x ".$thetables." = ".$result."</li>";
   echo "</ul>";
   echo "<h1>Choose Your Tables</h1>\n";
   echo "<a href='tables.php?times=2'>2 Times Table</a>";

Save this file as tables.php and give it a test. Then edit the else clause to use a for loop to produce hyperlinks for each of the tables from 2 to 12. A for loop in PHP is similar to other C family languages. It comes in 3 parts,

for (expression1;expression2;expression3)


To make a for loop of the kind you might have done in BASIC, the expressions are set as follows,

Expression 1 is the starting value for your stepper variable. $i=0 means use $i as a stepper variable and set its starting value to 0.

Expression 2 is the stopping point for your loop. $i<=10 means stop when $i is no longer less than equal to 10.

Expression 3 is the equivalent of STEP in BASIC. It tells your loop how to count. $i++ is the short hand for $i=$i+1. It means add 1 to $i on each iteration.

More Complex Than That

For loops are a little more interesting than the previous explanation suggests. You get that on this page because you are most likely going to be using for loops that work like that more often than not. However, the fact that the loops consist of 3 parts, each of which is an expression rather than a direct instruction to count, makes quite a difference to what can be done with a loop like this.

In reality, it is more accurate to say that Expression 1 is evaluated once, at the beginning of the loop. This is an unconditional evaluation, it happens no matter what else is going on in the loop.

Expression 2 is evaluated at the start of each iteration. If it evaluates to true, the loop proceeds and the statements that are nested in the curly braces are executed. If it evaluates to false, the loop stops.

Expression 3 is evaluated at the end of each iteration.

Even more interesting is that it is possible to have multiple expressions in each part. You separate each expression with a comma. For the second expression, if you use multiple expressions, they are all evaluated. The outcome of the last expression is the only one that is used to decide whether or not to proceed with the loop.

It's also possible to leave out some of the expressions. You might miss out the first expression if all of the important variables had previously been assigned values. You could leave out the second expression if you intend the loop to end with a break; statement. PHP assumes a value of true if the second expression is blank. Clearly the third expression could be left out if the condition that ends the loop can be met through the statements nested in the loop.

The following code works nicely,

$i = 1;
for (; ; )
   if ($i > 10)
   echo $i;

Clearly, the humble for loop takes on a new life in the C family languages. It's hard not to respect anyone who wants to explore the different ways you can exploit the features of these loops. Don't feel guilty if your inner geek takes over and has you working out weird and wonderful ways to use loops or comparing the performance you get from different approaches to the producing the same output.

Last Point About For

The format of the for loop allows for an easy way to produce alphabetic sequences.

for ($letter='A';$letter!='AA';$letter++)
   echo $letter;
for ($letter='a';$letter!='aa';$letter++)
   echo $letter;

Notice that $letter++ increases the letter by 1 and then starts doubling up letters at the end of the alphabet. The stopping condition uses the not equal to operator. You can explore the impact of incrementing the variable like this before you go and create an infinite loop. Assign the variable before a loop which uses a different stepper variable and iterates say, 200 times. Put the increment statement inside the loop and echo the letter variable each time.

A couple of simple ways to play about with this might be to find the neatest way to output lower case then upper case (ab...zAB...Z), or alternate lower and upper case (aAbBcC...zZ).