Loops

  • Braces are optional (for single-statement blocks).

While loop

while (expr) {
statements;
}
while (expr):
    statements;
endwhile;

Do ... while

do {
  statements;
} while (expr);

For loop

for (initial-expr; cond-expr; incr-expr) {
  statements;
}
for (initial-expr; cond-expr; loop-expr):
    statements;
endfor;

Foreach loop

  • Available as of PHP 4
  • Foreach operates on a copy of the specified array, not the array itself; changes to the array element returned are not reflected in the original array.
foreach ($arr as $value) {
    statements;
}
foreach ($arr as $key => $value) {
    statements;
}
foreach ($arr as $value):
    statements;
endforeach;

Jump statements

  • break [n]  : Break from the innermost n loops (the default is 1).
  • continue [n]  : Continue on to the next iteration of the innermost n loops (the default is 1).