Loopy Control Structures

Posted Thursday 4th May, 2006

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):

  1. for
  2. foreach (for-in in Javascript)
  3. while
  4. 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…


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:


for ($i = 0; $i < 10; $i++)
  echo $i.'<br/>';


for (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)

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:


for ($i = 0, $j = count($my_array); $i < $j; $i++)
  echo $my_array[$i].'<br/>';


for (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):


for ($i = 0, $j = count($my_array); $i < $j; echo $my_array[$i].'<br/>', $i++);


for (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…


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)

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)

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:

while (list($key, $value) = each(array_expression))


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)

“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…


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)

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!


The do-while loop is an alternative to while and, although practically identical, there is one major difference. Here’s the syntax:

} 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.


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.

Included in: Development, JavaScript, PHP, Web


  1. Accessibility
  2. Agile
  3. Ajax
  4. Apache
  5. API
  6. Architecture
  7. Books
  8. Browsers
  9. CMS
  10. CouchDB
  11. CSS
  12. Design
  13. Development
  14. Django
  15. Email
  16. Events
  17. Gaming
  18. Grammar
  19. Hardware
  20. HTML
  21. HTTP
  22. Humour
  23. Idea
  24. Information Architecture
  25. JavaScript
  26. jQuery
  27. Lean
  28. Life
  29. Linux
  30. Literature
  31. Mac OS X
  32. Management
  33. Meme
  34. Microformats
  35. Monday
  36. MySQL
  37. Networking
  38. News
  39. Personal
  40. Photoshop
  41. PHP
  42. Process
  43. Python
  44. Reference
  45. REST
  46. Science
  47. SEO
  48. Server
  49. Site
  50. Sitepimp
  51. Social
  52. Spelling
  53. Syndication
  54. Testing
  55. The Future
  56. Thoughts
  57. Tools
  58. Tutorial
  59. Tutorials
  60. Typography
  61. UI
  62. UNIX
  63. Virtualisation
  64. Web
  65. Web Standards
  66. Widgets
  67. Wii
  68. Writing
  69. Xbox
  70. XHTML