for

for loops are the most complex loops in PHP.
They behave like their C counterparts. The syntax of a
for loop is:

for (expr1; expr2; expr3)
statement

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

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.

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

Each of the expressions can be empty or contain multiple
expressions separated by commas. In expr2, all
expressions separated by a comma are evaluated but the result is taken
from the last part.
expr2 being empty means the loop should
be run indefinitely (PHP implicitly considers it as
TRUE, like C). This may not be as useless as
you might think, since often you'd want to end the loop using a
conditional break
statement instead of using the for truth
expression.

Consider the following examples. All of them display the numbers
1 through 10:

<?php/* example 1 */

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

/* example 2 */

for ($i = 1; ; $i++) { if ($i > 10) { break; } echo $i;}

/* example 3 */

$i = 1;for (; ; ) { if ($i > 10) { break; } echo $i;$i++;}

/* example 4 */

for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);?>

Of course, the first example appears to be the nicest one (or
perhaps the fourth), but you may find that being able to use empty
expressions in for loops comes in handy in many
occasions.

PHP also supports the alternate "colon syntax" for
for loops.

for (expr1; expr2; expr3):
statement
...
endfor;

It's a common thing to many users to iterate through arrays like in the
example below.

<?php/* * This is an array with some data we want to modify * when running through the for loop. */$people = array( array('name' => 'Kalle', 'salt' => 856412), array('name' => 'Pierre', 'salt' => 215863));

The above code can be slow, because the array size is fetched on
every iteration. Since the size never changes, the loop be easily
optimized by using an intermediate variable to store the size instead
of repeatedly calling count():

The point about the speed in loops is, that the middle and the last expression are executed EVERY time it loops.
So you should try to take everything that doesn't change out of the loop.
Often you use a function to check the maximum of times it should loop. Like here:

This note should might be under the "strlen" manual page, but there is a better chance for more paying attention here (nevertheless I have made a short note over there allso).

A loop function that test for the string length at each iteration takes forever (possibly due to "strlen" searches for the C-style string terminator - a binary 0 - every time..

So loops like this, using "strlen" in the for...

for ($i = 0; $i < strlen($crc); $i++) .....

Will benefit tremendously in speed by a short step that saves the string length once and use that in the loop.

$clen = strlen($crc);

for ($i = 0; $i < $clen ; $i++) .....

Note: as a real hard-core programmer You are aware, that this is only valid if you don't change the string content inside the loop (and hereby allso the length). If the change is only occationly , You could just refresh the length variable or else just live with a quite slow loop.

This "discovery" was made from using an example of 16 bit crc calculation over at the "crc32" function manual page, that do exactly that..

For those who are having issues with needing to evaluate multiple items in expression two, please note that it cannot be chained like expressions one and three can. Although many have stated this fact, most have not stated that there is still a way to do this: