Home  >  Article  >  Backend Development  >  Introduction to the difference between the usage of php loop statements for() and foreach()_PHP Tutorial

Introduction to the difference between the usage of php loop statements for() and foreach()_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:16:38926browse

The for loop is the most complex loop structure in PHP. Its behavior is similar to that of C language. The syntax of a for loop is:

for (expr1; expr2; expr3) statement
The first expression (expr1) is evaluated unconditionally once before the loop starts.
expr2 is evaluated before each loop. 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 (executed) after each loop.
Every expression can be empty. An empty expr2 means that the loop will continue indefinitely (like C, PHP considers its value to be TRUE). This may be less useful than you think, because you will often want to use a break statement to end the loop instead of using the for expression truth check.
Consider the following example. They both display the numbers 1 to 10:

Copy the code The code is as follows:

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 that using an empty expression in the for loop It will be very convenient in many situations.

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

for (expr1; expr2; expr3): statement; ...; endfor;
Other languages ​​have a foreach statement to traverse arrays or hash tables, and PHP also works (see foreach). In PHP 3, you can combine the list() and each() functions with a while loop to achieve the same effect. See the documentation for these functions for examples. foreach
PHP 4 (not PHP 3) includes the foreach construct, much like Perl and other languages. This is just a convenient way to iterate over an array. foreach can only be used with arrays, and an error will occur when trying to use it with other data types or an uninitialized variable. There are two syntaxes, the second being a less important but useful extension of the first.

foreach (array_expression_r_r as $value) statement foreach (array_expression_r_r as $key => $value) statement
The first format iterates over the given array_expression_r_r array. Each time through the loop, the value of the current cell is assigned to $value and the pointer inside the array is moved forward one step (so the next cell will be obtained in the next loop).
The second format does the same thing, except that the key value of the current cell will also be assigned to the variable $key each time through the loop.
Note: When foreach starts executing, the pointer inside the array will automatically point to the first unit. This means 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. Therefore, even with the construction of each(), the original array pointer has not changed, and the value of the array unit has not been affected.
Note: foreach does not support the ability to use "@" to suppress error messages.
You may have noticed that the following code functions exactly the same:
Copy the code The code is as follows:

$arr = array("one", "two", "three");
reset ($arr);
while (list(, $value) = each ($arr)) {
echo "Value: $value
n";
}
foreach ($arr as $value) {
echo "Value: $value
n";
}
?>

The following code has the same function:
Copy code The code is as follows:

reset ($arr);
while (list($key, $value) = each ($arr)) {
echo "Key: $key; Value: $ value
n";
}
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value
n";
}
?>

More examples of demonstration usage:
Copy code The code is as follows:

$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 "$v2n";
}
}
foreach (array(1, 2, 3, 4, 5) as $v) {
print " $vn";
}
?>

Copy code The code is as follows:

//foreach
$tar = array (
1 => 'East',
2 => 'West',
3 => 'South',
4 => 'North',
5 => 'Southeast',
6 => 'Southwest',
7 => 'Northeast',
8 => 'Northwest',
9 => 'North-South',
10 => ' stuff',
);
$TM = 'west';
foreach( $tar as $v=>$vv )
{
if( $vv == $TM )
{
echo $vv.'-'.$v.'
';
break;
}
//echo $vv;
}

//West-2
//for
Copy code The code is as follows:

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

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325875.htmlTechArticleThe 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 in the loop...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn