for
The for loop is a very complex loop in PHP. It behaves like in C language. The following is the syntax for a loop:
for (expr1; expr2; expr3) statement
The first expression (expr1) is the value at which the loop starts unconditionally.
At the beginning of each iteration, the expression expr2 is evaluated. If the value is TRUE, the loop continues and nested statements are executed. If the value is FALSE, the expression expr3 is evaluated (executed) at each iteration of
after executing the loop.
Every expression can be null. An empty expr2 means that the loop will run indefinitely (PHP implicitly assumes this to be true, like C). This may not end as you think, you can use the website construction server script class PHPPHP User Manual fancycontrol-structures.break.html>break statement to replace the for fact expression to end the loop.
Consider the following example. They will display numbers from 1 to 10:
/* Example 1 */for ($i = 1; $i 10) { break; } print $i;}/* Example 3 */$i = 1;for (;;) { if ($i > 10) { break; } print $i; $i++;}/* Example 4 */for ($i = 1; $i Of course, the first example is simplistic (or the fourth), but you can see that there are many opportunities for you to use empty expressions.
PHP still supports the "colon syntax" for loops.
for (expr1; expr2; expr3): statement; ...; endfor;
Another my language has a foreach statement to operate on arrays or hashes. There is no such construct in PHP 3; PHP 4 does (see foreach). In PHP 3, one can use the while, list() and each() functions to accomplish the same function. See the documentation for these functions.