for"
loop, the whole purpose of which is to repeatedly do something
until a given condition is true. In some ways it's kind of like
a repeating "if" statement as shown in the example
below:
for($counter=0;$counter<10;$counter++)
{
print $counter . "\n";
}
do-while loop operators,
and just as they sound by their names, they keep performing a
given task until something is true, eg.
$stop = 0;
while($stop != 1)
{
Print "Still running\n";
$stop = someFunctionThatReturns1or0();
}
if' and
yes you can group using && and || too, and then we have a block
of code inside our curly brackets, that keeps going until our
fictitious function returns a 1.
While can check anything you like in exactly the
same fashion, EG:
$line = getNextLine();
while($line != "peter")
{
$line = getNextLine();
}
getNextLine"
until the contents of $line are equal to peter.
NOTE: the "getNextLine" and
"someFunctionThatReturns1or0" calls are for example
only, so don't just cut and paste them, they won't work because
they don't really exist.
Do-while is a reverse version of
while, and anything in the curly braces will be
executed at least once before the check to stop is encountered.
Using our example above:
do
{
$line = getNextLine();
} while($line != "peter");
while
loop you use will depend on exactly how your program logic is to
function.
switch' and dare I say it (Shudders at the
thought) 'goto', which in mine and probably most
professional programmers minds should never have been invented
in the first place. There are also a couple of variations on the
standard if statement that I've not covered. As always refer to
the PHP manual's section on control statements at http://phpbuilder.com/manual/en/language
.control-structures.php and experiment with the others. I
would also strongly recommend reading the user submissions in
this section, even if you haven't in any of the others, some of
the tips and shortcuts in this section show some extremely cool
tricks that make PHP come alive.