PHPBuilder - Loops & Decisions in PHP - The ABC's of PHP Part 8 Page 2

RSS Twitter

Loops & Decisions in PHP - The ABC's of PHP Part 8 - Page 2

by: PHP Builder Staff
May 13, 2009

1,2,3 hey look I'm counting
The second statement in our journey is the "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:

    print $counter . "\n";

At first glance this looks very complicated, but if we take it apart bit by bit, you'll see it suddenly makes a lot of sense:
  • For( - The statement start
  • $counter = 0; - Set the default start value of our counting variable
  • $counter<10; - The decision part that decides when to stop, translated into ='if' speak "if $counter is less than 10 keep going"
  • $counter++) - What to do to update the control variable (In this case add 1) =(we covered ++ and - in maths & numbers)
So, as you can see the loop contains 3 parts, an initialization part, a when to stop check, and an update part, all together providing you fine control over how many times something happens.
What if I don't know in advance how many there is going to be?
That's a perfectly valid question. You can't always know in advance how many of a given count there are going to be, or even where that count will start.
For this reason we have the while and 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();

Again, the layout is very simple. In the brackets we have a decision in exactly the same way as in an '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();

The while loop will keep calling "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:

    $line = getNextLine();
  } while($line != "peter");

As you can see, the first thing this does is prevent us from having to load our variable the first time, because we know that the code will run at least one time, which while loop you use will depend on exactly how your program logic is to function.
We covered the most commonly used control statements here, but there are other controls available in the language, such as '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 .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.
Until next time
Don't get stuck in a loop.

« Previous Page

Comment and Contribute

Your comment has been submitted and is pending approval.




(Maximum characters: 1200). You have characters left.