Take you to understand the PHP loop structure

Source: Internet
Author: User
One of the most powerful functions of a computer program is to repeat certain operations in accordance with the prescribed conditions. The cyclic structure can reduce the amount of repetitive writing of the source program, that is, when a given condition is established, a program segment is executed repeatedly until the condition is not established. A given condition is called a cyclic condition, and a repeating program segment is called a loop body, and a while loop is provided in PHP, Do-while FollowRing and for loop three kinds.

Cycle: In a limited condition, to do one thing at a reciprocating.
PHP for while Do...while
1.for
Format
For (start condition, termination condition, step) {
Circulation body;
}
Example:
300 Big Bell Temple--"Big Bell Temple
Start Termination event Interval
5 minutes from 22:00 <=
5:35-22:00 5-minute departure
5:40-22:00 5-minute departure
.....
21:55-22:00 5-minute departure
22:00 <=22:00 5-minute departure
22:05 <=22:00 False

for ($i =1; $i <=5; $i + +) {
echo $i. " <br/> ";
}
Loop nesting
9*9
2.while
Format
Starting condition
while (termination condition) {
Circulation body;
Step value;
}
Note: The no-write step loop turns into a dead loop
While and for execution are consistent
3.do...while ()
Format
Starting condition
do{
Circulation body;
Step value;
}while ();
Note: Do...while is executed once, regardless of whether the termination condition is true
4.break continue
Break Terminate Loop
Continue end of this cycle cycle body continue the next cycle
The For and do...while all change the loop state effectively.

While loop

whileThe loop needs to set a Boolean condition that, when the condition is true, executes a block of statements continuously until the condition is false. After the program executes to the while statement, the following actions are performed:

    • Evaluates the value of an expression to determine whether TRUE or not FALSE .

    • If the expression is FALSE , the while statement ends, and then while the statement after the statement is executed.

    • If the expression is TRUE , execute the while code block in the statement brace, and then go back to step 1th.

Demo

Do...while Cycle

do...whileis while very similar to a loop, except that the value of an expression is checked at the end of each loop. do...whileLoop statement must be executed once, because the result value of the expression is checked after each loop.

Demo

<?php   $count = 0;  Do {          echo $count;      $count + +;  }  while ($count < 10);? >

For loop

forThe Loop statement is intended for situations where you know exactly how many times a repetition is performed, that is, a for statement that defines the number of times a loop is predefined in a for statement. forstatements are separated by semicolons into three parts, that is, loop variable initialization, conditional expressions, and loop variables that are self-growing or self-decreasing. Initialization is an assignment statement, which is used to assign an initial value to a loop control variable; a conditional expression is a relational expression that determines when to exit the loop, and increments the loop control variable, in what way each loop is changed. When the program executes to the for statement, the following actions are performed:

    • When entering the for loop for the first time, assign the initial value to the loop control variable.

    • Depending on the outcome of the judging condition, you decide whether to continue the loop, and if the condition is true, the loop is resumed, and if the condition is false, the end loop executes the following statement.

    • After executing the statement in the loop body, the system changes the value of the loop control variable based on how the loop control variable is added or subtracted, and then goes back to step 2 to re-determine whether to continue the loop.

Demo

<?php for     ($i = 1;  $i <=;  $i + +)                    echo "This is the result of the <b> $i </b> recurring Output <br>";    >

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.