The for loop for PHP

Probably one of the most understated parts of most programming languages (more so than the if?), a simple for can solve hundreds of everyday programming problems. Trouble is it looks a bit complicated, so I’m going to break it down.

Why is it slightly odd?

Firstly, why does it seem to stand out from other PHP functions? Well, it’s heavily based on its C counterpart and uses a similar method. Another reason why it seems odd is because it almost always contains a variable called $i, which never seems to mean anything.

The i means imaginary unit – quite simply, it’s a made up number that doesn’t have any relevance outside this context. That’s why a for loop makes use of this number – it creates it, then disposes of it. It is entirely imaginary.

What’s the big picture?

You’ve probably seen something like this

for ($i = 0; $i <= 10; $i++)
{
    echo $i;
}

and wondered what’s going on. Well, this article explains what this all means.

  1. The first expression
  2. The second expression
  3. Finally, the third expression

First expression

From the PHP manual

The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop.

The first expression is the first part of the code, which above says $i = 0. Evaluating or execution means that bit of code is run. Unconditionally means it’ll do it no matter what’s happened (unless it’s not valid). The beginning of the loop means that before the loop starts running.

In the example above, I’ve set our imaginary number i to 0. You might want to start at 1 depending on what you’re doing. Normally arrays start with $array[0], so that’s why it’s normally 0.

Each expression is separated with a semi-colon ;, not a comma.

Second expression

From the manual again

In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.

Each iteration means each loop of the code. If the bit of code in the second part of the function is true then it’ll keep going. If it’s false, the loop will end.

In my example, I’ve set this to $i <= 10, which means when the variable $i is less than or equal to 10. We’ll play around with this in a minute, but this is probably the most confusing part of some for loops you may come across as it can get quite complicated (if you want it to).

Third expression

And finally from the manual once more

At the end of each iteration, expr3 is evaluated (executed).

Once one loop has been completed, this final expression is run.

In the example I’ve written above, this means $i++ – which is shorthand for $i = $i + 1, or simply $i plus 1.

A simple example

Here’s the code again from above, slightly tweaked to add a space after each echo.

for ($i = 0; $i <= 10; $i++)
{
    echo $i.' ';
}

If you ran that code, you’d get

0 1 2 3 4 5 6 7 8 9 10

Because the loop would start with the number 0 (expression 1), echo it, then add one (expression 3). Once the imaginary number got to 10, it would stop (expression 2).

Playing with comparison operators

We can use comparison operators to change the behaviour of expression 3. At the moment, we’re using $i <= 10 which means less than or equal to. If we dropped the equal to and just left it as less than, we’d get the same result but without the 10 as the code would stop once it finished 9.

You could switch it around so that the code would stop when the number was greater than 10, by using $i > 10 – so soon as 10 was echoed and the imaginary number jumper to 11, it would stop.

Using it in the real world

Using a pre-defined number is useful if you’re printing the number of months in a year, but for something that varies, you’ll want to use a variable instead of a static number.

$cars = array('Golf', 'A5', 'DB9', 'Continental GT');

for ($i = 0; $i < count($cars); $i++)
{
    echo $i;
}

In the above example, I’ve created an array of four parts. You can use the count() function to count the number of values in an array – it just returns a number, so it would be like writing $i <= 4, but if you were to add another car, you wouldn’t have to change the number.

Change echo $i; to echo $cars[$i]."\n"; and you’t get something like this:

Golf
A5
DB9
Continental GT

Hey presto! I’ll explore more real-world scenarios soon.