Loopy Control Structures
If ever there was a case for RTFM, it would be in conjunction with control structures in PHP (and Javascript too, since most of them are identical).
I’ve recently learnt a thing or two about loops in both of these languages and thought I would share it with the blog-o-sphere; in case you, like me, didn’t bother to read the manual properly.
There are four different types of specific looping control structure available in PHP and Javascript. These are (ordered by speed, slowest first):
for
foreach
(for-in
in Javascript)while
do-while
Obviously you can set up loops using any number of different techniques but it’s really only the specific looping control structures we’re interested in today.
Let’s take a closer look at each of these in turn…
for
The for
loop is a staple part of any programmer’s diet and has a great many uses in most systems. It is the most complicated of all the loops and, as a result, will need quite a bit of explanation.
Generally we’re used to seeing examples of it in use like so:
PHPfor ($i = 0; $i < 10; $i++) { echo $i.'<br/>'; }
Javascriptfor (var i = 0; i < 10; i++) { document.write(i + '<br/>'); }
It’s important to remember that even though the for
loop is pretty complicated and therefore slower than it’s compatriots, it can also be optimised to a far higher degree. To do that, we need to delve in a little deeper.
Here is the syntax for this type of loop:
for (expr1; expr2; expr3)
{
statement;
}
Let’s take a close look at those expressions, because that’s where the for
loop is hiding all of it’s power!
The first expression (expr1) is evaluated once unconditionally at the beginning of the loop.
The second expression (expr2) is evaluated at the beginning of each iteration of the loop. If the evaluation returns TRUE then the loop continues and the nested statement(s) are executed. However, if the expression evaluation returns FALSE the loop ends.
The third expression (expr3) is evaluated at the end of each iteration of the loop.
Okey dokey, is it all making sense so far? Jolly good, but here comes the really clever bit; each of those expressions can contain multiple expressions separated by commas! This means that you can speed up your loops by making use of those expressions correctly.
A good example would be iterating through an array where there is a need to obtain the length of the array:
PHPfor ($i = 0, $j = count($my_array); $i < $j; $i++) { echo $my_array[$i].'<br/>'; }
Javascriptfor (i = 0, j = my_array.length; i < j; i++) { document.write(my_array[i] + '<br/>'); }
And if you’re really paying attention, with that example we can speed it up further by doing this (careful with the semi-colons):
PHPfor ($i = 0, $j = count($my_array); $i < $j; echo $my_array[$i].'<br/>', $i++);
Javascriptfor (i = 0, j = my_array.length; i < j; document.write(my_array[i] + '<br/>'), i++);
Isn’t that nifty?
Anyway, I’ll leave you to play with that in your own time and continue on to explain the next loop type…
foreach
The foreach
loop, available in PHP and familiar to Perl programmers, allows simple iteration through an array using the internal pointer. It’s handy and it’s quick to initiate but it’s strictly arrays only here - pass it a variable of any other data type and it will error.
Let’s take a look at the syntax:
foreach (array_expression as $key => $value)
{
statement;
}
The structure will loop over the array given in array_expression. On each iteration of the loop, the value of the current element in the array (the element the internal pointer currently points to) will be returned in $value. The $key variable is optional and will contain the array key for that current element. The loop will then advance the internal array pointer by one.
In PHP 4, this kind of loop will only return the value of the current element - you will not be able to change it, only copy it. In PHP 5, however, you can pass $value by reference which will allow changes to be made. Syntax for this method is as follows:
foreach (array_expression as $key => &$value)
{
statement;
}
It’s the ampersand that’s important; this is called “referencing”. I’ll go over references in another blog post - for now, just take my word for it.
In regard to speed, the foreach
statement is generally as quick as a while
statement although it has a couple of extra overheads such as reseting the internal array pointer. It is functionally identical to the following:
reset(array_expression);
while (list($key, $value) = each(array_expression))
{
statement;
}
for-in
The syntax for foreach
in Javascript is a little different. To start with, the command is called for-in
and it returns only the key to the current element, not the value as well. At first, this can seem strange, but trust me - it makes perfect sense. Here’s the syntax:
for (var i in myArray)
{
document.write(myArray[i]);
}
“But wait,” I hear you cry, “what’s so special about that then?”
Well, for a start it doesn’t care whether your array is a plain old normal numerical array, an associative array or a collection of objects. It will handle all of the above; which you can imagine, makes it pretty bloomin’ useful! What’s more, because the value is simply being referenced differently (and not copied as in PHP), we can also apply as many changes as we want.
So there you have it; if you’re looping through arrays, always opt for foreach
or for-in
over for
where possible.
Let’s move on…
while
The while
loop is the simplest form of loop, and because of this, the fastest. The basic syntax, in both PHP and Javascript, is:
while (expr)
{
statement;
}
Basically, a while
loop tells either PHP or Javascript to run statement only while expr evaluates to TRUE.
Simple eh? No wonder it’s fast!
do-while
The do-while
loop is an alternative to while
and, although practically identical, there is one major difference. Here’s the syntax:
do
{
statement;
} while (expr);
The loop will always perform statement once and then it will evaluate expr. If expr evaluates to TRUE, the loop will iterate again. This is useful when you have a piece of code you always want to perform at least once.
Performance-wise, the do-while
loop is the same as a while
loop.
Summary
So there we have it - PHP and Javascript loops in a nutshell.
I sincerely hope I’ve managed to impart a little knowledge here. Some of this stuff can be incredibally useful and it’s surprising how many top-class programmers aren’t aware of the full ins-and-outs of the control structures available to them.
I’ll probably be following this post up with a look at conditional control structures including if
, switch-case
, the ternary conditional operator and some other nifty techniques.
For more information on looping control structures, take a look at “Control Structures” in the PHP manual or “Statements” in Sun’s Javascript manual.