In this article, we talk about the use and differences between the For loop and the Foreach Loop in PHP, and I believe many of you may not be aware of the difference between the for loop and the Foreach loop, so let's talk about it in detail today!
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 (EXPR1) is evaluated unconditionally once before the loop begins.
The EXPR2 is evaluated 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.
The EXPR3 is evaluated (executed) after each loop.
Each expression can be empty. Expr2 means an infinite loop (like C, PHP considers its value to be TRUE). This may not be as useless as you might think, because you will often want to use the break statement to end the loop instead of using the for expression to determine the truth.
Consider the following example. They all show numbers 1 through 10:
for ($i = 1; $i <=; $i + +) { print $i;} for ($i = 1;; $i + +) { if ($i >) {break ; } Print $i;} $i = 1;for (;;) { if ($i >) {break ; } Print $i; $i + +;} for ($i = 1; $i <=; print $i, $i + +);
Of course, the first example looks most normal (or fourth), but you may find it convenient to use empty expressions in a for loop in many situations.
PHP also supports substitution syntax for a for loop with colons.
for (EXPR1; expr2; expr3): statement; ...; ENDfor;
Other languages have a foreach statement to iterate over an array or hash list, and PHP is ok (see foreach). In PHP 3, you can combine the list () and each () function with a while loop to achieve the same effect. See the documentation for these functions for examples.
Foreach
PHP 4 (not PHP 3) includes a foreach structure, much like Perl and other languages. This is just a simple way to iterate through an array. foreach can only be used with arrays, and an error occurs when trying to use it for other data types or an uninitialized variable. There are two kinds of syntax, and the second is a useful extension that is minor but the first.
foreach (Array_expression_r_r as $value) statement foreach (array_expression_r_r as $key = $value) statement
The first format iterates through the given array of Array_expression_r_r. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (so the next cell in the next loop will be taken).
The second format does the same thing, except that the key value of the current cell is assigned to the variable $key in each loop.
Note: When foreach starts executing, the pointer inside the array automatically points to the first cell. This means that you do not need to call Reset () before the Foreach loop.
Note: Also note that foreach is manipulating a copy of the specified array, not the array itself. So even with each () construct, the original array pointer does not change, and the value of the array cell is unaffected.
Note: foreach does not support the ability to suppress error messages with "@".
You may have noticed that the following code functions exactly the same:
$arr = Array ("One", "one", "one", "three"), Reset ($arr), while (list (, $value) = each ($arr)) { echo ' value: $value <br>\n ";} foreach ($arr as $value) { echo "value: $value <br>\n";}
The following code functions are identical:
Reset ($arr), while (List ($key, $value) = each ($arr)) { echo ' key: $key; Value: $value <br>\n ";} foreach ($arr as $key = $value) { echo "key: $key; Value: $value <br>\n ";}
More examples of demonstration usage:
$a = Array (1, 2, 3,), foreach ($a as $v) { print "current value of \ $a: $v. \ n";} $a = Array (1, 2, 3, +); $i = 0;foreach ($a as $v) { print "\ $a [$i] + $v. \ n"; $i + +;} $a = Array ( "one" = 1, "One" = 2, "three" = 3, "Seventeen"); foreach ($a as $k = = $ V) { print "\ $a [$k] = $v. \ n";} $a [0][0] = "a"; $a [0][1] = "B"; $a [1][0] = "Y"; $a [1][1] = "Z"; foreach ($a as $v 1) { foreach ($v 1 as $v 2) { print "$v 2\n "; }} foreach (Array (1, 2, 3, 4, 5) as $v) { print "$v \ n";}
Foreach$tar = Array ( 1 = ' East ', 2 = ' West ', 3 = ' South ', 4 = ' North ', 5 = ' Southeast ', 6 = > ' Southwest ', 7 = ' Northeast ', 8 = ' Northwest ', 9 = ' North and South ', ten = ' things ', $TM = ' West '; foreach ($tar as $v =&G T $VV) { if ($vv = = $TM) { echo $vv. '-' . $v. ' <br/> '; break; } echo $VV;}
For
echo ' <br/> '; for ($i = 1; $i <= count ($tar), $i + +) { if ($tar [$i] = = $TM) { echo $tar [$i]. '-' . $i. ' <br/> '; break; }}
Summary: foreach is identical to the for result, but in an efficient foreach win with for, the home for needs to know the array length and then use $i++ to manipulate, page foreach does not need to know the array length can automatically detect and enter key, and value.
Related recommendations:
How to understand the difference between a for and foreach two loop structure traversal array in PHP
Sample code sharing for loop in PHP with return