Introduction of php loop statements for of and foreach of

  • 2020-05-19 04:23:44
  • OfStack

The for loop is the most complex loop structure in PHP. Its behavior is similar to that of 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.
expr2 is evaluated before the start of each loop. If the value is TRUE, the loop continues, executing the nested loop statement. If the value is FALSE, the loop is terminated.

expr3 is evaluated (executed) after each loop.
Each expression can be empty. expr2 being empty means that it will loop forever (like C 1, which PHP considers to be TRUE). This may not be as useful as you might think, because you will often want to end the loop with an break statement instead of using the truth value of an for expression.
Consider the following example. They all show the Numbers 1 to 10:
 
<?php 
for ($i = 1; $i <= 10; $i++) { 
print $i; 
} 
for ($i = 1; ; $i++) { 
if ($i > 10) { 
break; 
} 
print $i; 
} 
$i = 1; 
for (;;) { 
if ($i > 10) { 
break; 
} 
print $i; 
$i++; 
} 
for ($i = 1; $i <= 10; print $i, $i++); 
?> 

Of course, the first example looks the most normal (or the fourth), but you may find it convenient to use empty expressions in for loops in many situations.

PHP also supports an alternative syntax for for loops with colons.

for (expr1; expr2; expr3): statement; ...; endfor;
Other languages have foreach statements for traversing groups or hash tables, as well as PHP (see foreach). In PHP 3, you can combine the list() and each() functions with an while loop to achieve the same effect. See documentation for examples of these functions. foreach
PHP 4 (not PHP 3) includes the foreach structure, much like Perl and other languages. This is just a simple way to go through groups. foreach can only be used with arrays, and an error occurs when you try to use it with another data type or with an uninitialized variable. There are two syntaxes, the second being a minor but useful extension to 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_expression_r_r array. In each loop, the value of the current cell is assigned to $value and the pointer inside the array moves forward 1 step (so the next loop will give you the next cell).
The second format does the same thing, except that the key value of the current cell is also assigned to the variable $key in each loop.
Note: when foreach starts executing, the pointer inside the array will automatically point to the first cell. This means that there is no need to call reset() before the foreach loop.
Note: also note that foreach operates on a copy of the specified array, not the array itself. So even with the each() construct, the pointer to the original array does not change, and the value of the array cell is not affected.
Note: foreach does not support the ability to disable error messages with "@".
You may have noticed that the following code does exactly the same thing:
 
<?php 
$arr = array("one", "two", "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 exactly the same:
 
<?php 
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 usage examples:
 
<?php 
$a = array (1, 2, 3, 17); 
foreach ($a as $v) { 
print "Current value of \$a: $v.\n"; 
} 
$a = array (1, 2, 3, 17); 
$i = 0; 
foreach ($a as $v) { 
print "\$a[$i] => $v.\n"; 
$i++; 
} 
$a = array ( 
"one" => 1, 
"two" => 2, 
"three" => 3, 
"seventeen" => 17 
); 
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 $v1) { 
foreach ($v1 as $v2) { 
print "$v2\n"; 
} 
} 
foreach (array(1, 2, 3, 4, 5) as $v) { 
print "$v\n"; 
} 
?> 

 
//foreach 
$tar = array ( 
1 => ' In the east ', 
2 => ' In the west ', 
3 => ' south ', 
4 => ' north ', 
5 => ' In the southeast ', 
6 => ' southwest ', 
7 => ' The northeast ', 
8 => ' The northwest ', 
9 => ' The north and the south ', 
10 => ' things ', 
); 
$TM = ' In the west '; 
foreach( $tar as $v=>$vv ) 
{ 
if( $vv == $TM ) 
{ 
echo $vv.'-'.$v.'<br />'; 
break; 
} 
//echo $vv; 
} 

/ / west - 2
//for
 
echo '<br />'; 
for( $i=1;$i<=count( $tar ) ;$i++ ) 
{ 
if( $tar[$i] == $TM ) 
{ 
echo $tar[$i].'-'.$i.'<br />'; 
break; 
} 
} 

/ / west - 2
Conclusion: the results of foreach and for are exactly the same, but foreach is better than for in efficiency. The first page for needs to know the array length and then use $i++ to operate. The second page foreach does not need to know the array length and can automatically detect and enter key and value.

Related articles: