Loop Structures in PHP

for loop. The "loop to" value (in this case 10) can be a number, variable, count($array), strlen($string), or any other function that produces a number.

for ($i=0; $i<10; $i++) {
    // do something
}

Example:

PHP
for ($i=0; $i<10; $i++) {
    print $i . ", ";
}

Produces the result: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 


foreach loop without key. This is used to iterate over arrays.

foreach ($array as $item) {
    // do something
}

Example:

PHP
$array = (0,1,2,3,4,5,6,7,8,9);
foreach ($array as $item) {
    print $item . ", ";
}

Produces the result: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 


foreach with key. This is used to iterate over arrays but also accessing the array key. 

Example:

foreach ($array as $i => $item)
    // do something
}

PHP
$array = (a,b,c,d,e,f,g,h,i,j);
foreach ($array as $i => $item)
    print $i . " = " . $item . ", ";
}

Produces the result: a = 0, b = 1, c = 2, d = 3, e = 4, f = 5, g = 6, h = 7, i = 8, j = 9, 


Loop 4. The basic while loop. This loop can be used in the same way as the for loop. The "loop to" value can be a number or any function that produces a number.

$i = 0;
while ($i < 10) {
    // do something
    $i++;
}

Example:

PHP
$i = 0;
while ($i < 10) {
    print $i . ", ";
    $i++;
}

Produces the result: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 


do loop. This loop can be used in the same way as the for loop. The "loop to" value (10) can be a number or any function that produces a number. The difference is that the loop is executed before the comparison, therefore it will always execute at least once.

$i = 0;
do {
    // do something
} while (++$i < 10);

Example:

PHP
$i = 0;
do {
    print $i . ", ";
} while (++$i < 10);

// Note $i needs to be incremented before the comparison, therefore use ++$i instead of $i++
// An alternate way to do it would be to increment $i within the loop, then do straight comparision like this ($i < 10)

Produces the result: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 


do loop with break. This loop can be used in the same way as the for loop. The "loop to" value can be a number or any function that produces a number.

$i = 0;
do {
    // do something
    if (++$i > 10) { break; } // breaks out of the loop
} while (true);

Example:

PHP
$i = 0;
do {
    print $i . ", ";
    if (++$i > 10) { break; } // breaks out of the loop
} while (true);

// This could be useful if many conditions should cause the loop to terminate. Just make sure at least one condition will always make it terminate or you may have an infinite loop.

Produces the result: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 


goto loop. Create a label, then goto label; You also cannot jump into any sort of loop or switch structure. You may jump out of loops or switch. The gogo is useful to simplify multi-level break statements.

$i = 0;
loop: {
    // do something
} if (++$i < 10) { goto loop; }

Example:

PHP
$i = 0;
loop: {
    print $i . ", ";
} if (++$i < 10) { goto loop; }
print "<br>Done.";

Produces the result:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
Done.