PHP Loop statement for () and foreach () use a different description _php tips

Source: Internet
Author: User
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 before the start of the loop.
EXPR2 is evaluated before each loop starts. If the value is TRUE, the loop continues and the nested loop statement executes. If the value is FALSE, the loop is terminated.

The EXPR3 is evaluated after each loop (execution).
Each expression can be empty. Expr2 is empty means an infinite loop (like C, PHP thinks its value is TRUE). This may not be as useless as you might think, because you often want to use the break statement to end the loop instead of using a for expression truth value.
Consider the following examples. They all show numbers 1 through 10:
Copy Code code as follows:

<?php
for ($i = 1; $i <= $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 <= print $i, $i + +);
?>

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

PHP also supports the substitution syntax for loops with colons.

for (EXPR1; expr2; expr3): statement; ...; ENDfor;
Other languages have a foreach statement to traverse an array or hash list, and PHP is ok (see foreach). In PHP 3, you can combine the list () and each () function to achieve the same effect with a while loop. Examples see the documentation for these functions. Foreach
PHP 4 (not PHP 3) includes a foreach structure, which is similar to Perl and other languages. This is just a simple way to traverse an array. foreach can only be used in arrays, and an error occurs when you try to use them for other data types or for an uninitialized variable. There are two types of syntax, the second is a useful extension that is minor but is the first.

foreach (Array_expression_r_r as $value) statement foreach (Array_expression_r_r as $key => $value) statement
The first form traverses the given Array_expression_r_r array. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array moves forward one step (so the next cell will be taken in the next loop).
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 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. Therefore, even if there is a construct of each (), the original array pointer does not change and the value of the array cell is unaffected.
Note: foreach does not support the ability to use ' @ ' to suppress error messages.
You may notice that the following code functions exactly the same:
Copy Code code as follows:

<?php
$arr = Array ("One", "two", "three");
Reset ($arr);
while (the list (, $value) = each ($arr)) {
echo "Value: $value <br>\n";
}
foreach ($arr as $value) {
echo "Value: $value <br>\n";
}
?>

The following code features are exactly the same:
Copy Code code as follows:

<?php
Reset ($arr);
while (the 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 the use of demonstrations:
Copy Code code as follows:

<?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 $v 1) {
foreach ($v 1 as $v 2) {
Print "$v 2\n";
}
}
foreach (Array (1, 2, 3, 4, 5) as $v) {
print "$v \ n";
}
?>

Copy Code code as follows:

Foreach
$tar = Array (
1 => ' East ',
2 => ' West ',
3 => ' Nam ',
4 => ' North ',
5 => ' SE ',
6 => ' Southwest ',
7 => ' ne ',
8 => ' Northwest ',
9 => ' North and South ',
Ten => ' Things ',
);
$TM = ' West ';
foreach ($tar as $v => $vv)
{
if ($VV = = $TM)
{
echo $vv. '-$v. ' <br/> ';
Break
}
Echo $VV;
}

West-2
For
Copy Code code as follows:

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

West-2
Summary: foreach and for results are exactly the same, but in the efficiency foreach to win with for, home for need to know the length of the array and then use $i++ to operate, page foreach does not need to know the length of the array can automatically detect and enter the key, and value.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.