PHP Loop Statement Depth Understanding Analysis--while, for, foreach, do While

Source: Internet
Author: User

Loop structure
one, while Loop
while (expression)
{
The loop body;//executes repeatedly until the expression is false
}

Code:
$index = 1;
while ($index <5)
{
Print "number is {$index}
";
$index + +;
}
print "Done";
Operation Result:
Number is 1
Number is 2
Number is 3
Number is 4
Done

second, do and loop
Do {
the loop body;//executes repeatedly until the expression is False
} while (expression)

Code:
Do {
$index + +;
print "number is {$index}
";
} while ($index <0);
print "Done";
Operation Result:
Number is 1
Done

There is a difference between the Do while Loop statement and while, and the difference is that the do and regardless of whether the condition is true will be executed first, while the while must be true to execute once.

Third, for Loop
There are two types of loops depending on the loop condition
One: Count loops (general use for)
Another: Conditional loops (typically using while Do-while)
For (EXPR1; expr2; expr3) {
Statement
 }
where the EXPR1 is the initial value of the condition. EXPR2 is a condition for judging, usually with a logical operation symbol (logical operators)
when judging the conditions. EXPR3 the part to be executed after the execution of the statement, to change the condition for the next cycle judgment, such as add one. Wait a minute. and statement
for a qualifying execution part program, if the program has only one row, you can omit the curly brace {}.
The following example is a "dare not" example written with a for loop, which can be compared with a while loop.
<?php
For ($i =1; $i <=5; $i + +) {
echo "$i. Not anymore.
n ";
 }
?>Operation Result:
1. Not anymore.
2. Not anymore.
3. Not anymore.
4. Not anymore.
5. Not anymore.

Four, foreach Loop

The foreach statement is used to iterate through an array. For Each loop, the value of the current array element is assigned to the value variable (the array pointer moves one at a time)-and so on

Syntax:
foreach (array as value)
{
code to be executed;
}
Code:
<?php
$arr =array ("One", " One", "one", "three");

foreach ($arr as $value)
{
echo "Value:". $value. "
";
}
?>Operation Result:
Value:one
Value:two
Value:three
The above is PHP in four kinds of circulating body, according to different conditions to choose the appropriate circulation body application

PHP Loop Statement Depth Understanding Analysis--while, for, foreach, do While

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.