PHP Loop statement for () and foreach () Usage differences Introduction _php tutorial

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 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:
Copy CodeThe code is as follows:
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 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:
Copy CodeThe code is as follows:
$arr = Array ("One", "one", "three");
Reset ($arr);
while (list (, $value) = each ($arr)) {
echo "Value: $value
\ n ";
}
foreach ($arr as $value) {
echo "Value: $value
\ n ";
}
?>

The following code functions are identical:
Copy CodeThe 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 CodeThe 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,
"Both" = 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 CodeThe 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 and South ',
' Things ',
);
$TM = ' West ';
foreach ($tar as $v = $VV)
{
if ($VV = = $TM)
{
echo $vv. '-' $v. '
';
Break
}
Echo $VV;
}

West-2
For
Copy CodeThe code is as follows:
Echo '
';
for ($i =1; $i <=count ($tar); $i + +)
{
if ($tar [$i] = = $TM)
{
echo $tar [$i]. ' -'. $i. '
';
Break
}
}

West-2
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.

http://www.bkjia.com/PHPjc/325875.html www.bkjia.com true http://www.bkjia.com/PHPjc/325875.html techarticle 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) in the loop ...

  • 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.