This article mainly introduces the PHP process control for the statement, has a certain reference value, now share to everyone, the need for friends can refer to
This paper tries to use the basic Learning Master, please close this page
This article reads 7 minutes, the understanding difficult person is not good to say?
(PHP 4, PHP 5, PHP 7)
The For loop is the most complex loop structure in PHP. Its behavior is similar to the C language. The syntax for the For loop is:
for (EXPR1; expr2; expr3) statement
The first expression () is evaluated unconditionally (and executed) one time before the start of the loop.
Evaluates before each cycle begins. If the value is TRUE , the loop continues and the nested loop statement is executed. If the value is FALSE , the loop is terminated.
evaluated (and executed) after each loop.
Each expression can be empty or include multiple expressions separated by commas. In an expression, all expressions separated by commas are evaluated, but only the last result is taken. Null means an infinite loop (like C, which PHP secretly considers to be the value TRUE ). This may not be as useless as you might think, because you often want to end a loop with a conditional break statement instead of using the for expression truth.
Consider the following examples, which show numbers 1 through 10:
<?php/* Example 1 */for ($i = 1; $i <=, $i + +) { echo $i;} /* Example 2 */for ($i = 1;; $i + +) { if ($i >) {break ; } echo $i;} /* Example 3 */$i = 1;for (;;) { if ($i >) {break ; } echo $i; $i + +;} /* Example 4 */for ($i = 1, $j = 0; $i <=; $j + = $i, print $i, $i + +); >
Of course, the first example looks the most concise (or someone thinks it's the fourth one), but the user may find it convenient to use an empty expression in a for loop for many occasions.
PHP also supports substitution syntax for a for loop with colons.
for (EXPR1; expr2; expr3): statement; ... endfor;
Sometimes it is often necessary to traverse an array as in the following example:
<?php/* * This array will change the value of some of these cells during traversal */$people = array ( ' name ' = ' Kalle ', ' salt ' = = 856412), Array (' Name ' = ' Pierre ', ' salt ' = 215863) ); for ($i = 0; $i < count ($people), + + $i) { $people [$i] [' salt '] = Rand (000000, 999999);}? >
The above code may perform very slowly, because the length of the array is computed once for Each loop. Since the length of the array is always constant, you can use an intermediate variable to store the length of the array to optimize instead of constantly calling count ():
<?php$people = Array ( ' name ' = ' Kalle ', ' salt ' = ' 856412 '), Array (' name ' = ' Pierre ', ' salt ' =& Gt 215863) ), for ($i = 0, $size = count ($people), $i < $size, + + $i) { $people [$i] [' salt '] = rand (000000, 999999);}? >
Related recommendations:
Do-while of PHP Process Control
PHP Process Control Elseif/else if