A for loop is the most complex loop structure in PHP. Its behavior is similar to that in C. The syntax of the for loop is:
For
(PHP 4, PHP 5)
A for loop is the most complex loop structure in PHP. Its behavior is similar to that of C language. The syntax of the for loop is:
for (expr1; expr2; expr3) statement
The first expression (expr1) is unconditionally evaluated (and executed) before the loop starts.
Expr2 is evaluated before each cycle starts. If the value is TRUE, the loop continues and the nested loop statement is executed. If the value is FALSE, the loop is terminated.
Expr3 is evaluated (and executed) after each loop ).
Each expression can be null or multiple expressions separated by commas. In expression expr2, all expressions separated by commas are calculated, but only the last result is obtained. If expr2 is null, it means an infinite loop (like C, PHP secretly thinks its value is TRUE ). This may not be as useful as imagined, because it is often expected that conditional break statements are used to end the loop rather than true value judgment using the for expression.
Consider the following example where they all show numbers 1 to 10:
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 looks the most concise (or someone thinks it is the fourth), but users may find it convenient to use null expressions in a for loop in many cases.
PHP also supports the replacement syntax of the for loop with colons.
for (expr1; expr2; expr3): statement; ...endfor;
Sometimes we often need to traverse the array like the following example:
'Kalle', 'Salt' => 856412), Array ('name' => 'Pierre ', 'Salt' => 215863); for ($ I = 0; $ I <sizeof ($ people); ++ $ I) {$ people [$ I] ['Salt'] = rand (000000,999 999);}?>
The above code may be executed very slowly, because the length of the array needs to be calculated once every loop. Since the length of the array remains unchanged, you can use an intermediate variable to store the length of the array to optimize rather than constantly calling count ():
'Kalle', 'salt' => 856412), Array('name' => 'Pierre', 'salt' => 215863) );for($i = 0, $size = sizeof($people); $i < $size; ++$i){ $people[$i]['salt'] = rand(000000, 999999);}?>
Foreach
(PHP 4, PHP 5)
The foreach syntax structure provides a simple way to traverse arrays. Foreach can only be applied to arrays and objects. if you try to apply it to variables of other data types or uninitialized variables, an error message will be sent. There are two syntaxes:
foreach (array_expression as $value) statementforeach (array_expression as $key => $value) statement
The first format traverses the given array_expression array. In each loop, the value of the current unit is assigned to $ value and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ).
In the second format, only the key names of the current unit are assigned to the variable $ key in each loop.
You can also customize the traversal object.
Note:
When foreach starts to execute, the pointer inside the array automatically points to the first unit. This means that you do not need to call reset () before the foreach loop ().
Because foreach depends on the internal array pointer, modifying its value in the loop may lead to unexpected behavior.
You can easily add & before $ value to modify the elements of the array. This method will assign values by reference instead of copying a value.
$ Value reference is only available when the retrieved array can be referenced (for example, a variable ). The following code cannot be run:
The $ value reference of the last element of the Warning array is retained after the foreach loop. We recommend that you use unset () to destroy it.
Note:
Foreach does not support "@" to suppress error messages.
You may notice that the following code functions are identical:
\n";}foreach ($arr as $value) { echo "Value: $value
\n";}?>
The following code has the same functions:
\n";}foreach ($arr as $key => $value) { echo "Key: $key; Value: $value
\n";}?>
More examples of demo usage:
$v.\n"; $i++;}/* foreach example 3: key and value */$a = array( "one" => 1, "two" => 2, "three" => 3, "seventeen" => 17);foreach ($a as $k => $v) { echo "\$a[$k] => $v.\n";}/* foreach example 4: multi-dimensional arrays */$a = array();$a[0][0] = "a";$a[0][1] = "b";$a[1][0] = "y";$a[1][1] = "z";foreach ($a as $v1) { foreach ($v1 as $v2) { echo "$v2\n"; }}/* foreach example 5: dynamic arrays */foreach (array(1, 2, 3, 4, 5) as $v) { echo "$v\n";}?>
Use list () to unpackage nested arrays
(PHP 5> = 5.5.0)
PHP 5.5 adds the function of traversing an array and unpacks the nested array into the loop variable. you only need to provide list () as the value.
For example:
The above routine will output:
A: 1; B: 2A: 3; B: 4
The number of elements in list () can be less than that of nested arrays. in this case, the extra array elements are ignored:
The above routine will output:
13
If the number of units listed in list () is more than the number of nested arrays, a message-level error message is sent:
The above routine will output:
Notice: Undefined offset: 2 in example.php on line 7A: 1; B: 2; C: Notice: Undefined offset: 2 in example.php on line 7A: 3; B: 4; C: