Deep understanding and analysis of PHP loop statements -- while, for, foreach, do while, whileforeach

Source: Internet
Author: User

Deep understanding and analysis of PHP loop statements -- while, for, foreach, do while, whileforeach

Loop Structure
 1. while Loop
While (expression)
{
Loop body; // execute repeatedly until the expression is false
}

Code:
$ Index = 1;
While ($ index <5)
{
Print "Number is {$ index}
";
$ Index ++;
}
Print "Done ";
Running result:
Number is 1
Number is 2
Number is 3
Number is 4
Done

2. do while LOOP
Do {
Loop body; // execute repeatedly until the expression is false
} While (expression)

Code:
Do {
$ Index ++;
Print "Number is {$ index}
";
} While ($ index <0 );
Print "Done ";
Running result:
Number is 1
Done

The Do While loop statements are different from while statements. The difference is that do while statements are executed first regardless of whether the condition is true or not, while While statements are executed only once.

3. for Loop
There are two types of loops based on different cyclic conditions.
One type: Counting loop (generally)
Another type: Conditional loop (usually while do-while)
For (expr1; expr2; expr3 ){
Statement
}
Expr1 is the initial value of the condition. Expr2 is the condition for judgment. Generally, it uses the logical operator number (logical operators)
When the condition is determined. Expr3 is the part to be executed after statement is executed. It is used to change the condition for the next loop judgment, such as adding one. While statement
For some programs that meet the conditions, if the program has only one line, you can omit braces {}.
The following example uses the for loop to write "do not dare later". You can compare it with the while loop.
<? Php
For ($ I = 1; $ I <= 5; $ I ++ ){
Echo "$ I. No more
N ";
}
?> Running result:
1. Do not dare later
2. Do not dare later
3. Do not dare later
4. Do not dare later
5. Do not dare later

4. foreach Loop

The foreach statement is used to traverse arrays cyclically. Each cycle is performed, the value of the current array element is assigned to the value variable (the array pointer is moved one by one)-and so on

Syntax:
Foreach (array as value)
{
Code to be executed;
}
Code:
<? Php
$ Arr = array ("one", "two", "three ");

Foreach ($ arr as $ value)
{
Echo "Value:". $ value ."
";
}
?> Running result:
Value: one
Value: two
Value: three
The above are four types of loop bodies in PHP. Select the corresponding loop bodies based on different conditions.

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.