PHP for While Loop statement Learning Note _php Tutorial

Source: Internet
Author: User
This article is to introduce the basic use of circular statements in PHP, including for Loop, while loop and do While loop use method, this article is very suitable for beginners PHP beginner Oh.

Use the For statement to control multiple variables, enabling advanced applications for multiple loops.

Here is a program that outputs the 9*9 multiplication table with a for statement:

The code is as follows Copy Code

for ($i =1; $i <=9; $i + +) {
for ($j =1; $j <= $i; $j + +) {
$sum = $i * $J;
echo $sum. " T ";
}
echo "
";
}
?>

In addition to the while Loop statement mentioned earlier, PHP provides the same functionality for a For loop statement. The For statement can implement a more complex and multifunctional loop, and any while loop can be replaced by a for loop.

Basic structure form:

for (expression 1; expression 2; expression 3) {
Execute Statement Body
}

The following procedures are performed:

1, first execute the expression 1;

2, then judge the true and false expression 2, if False, jump out for the next PHP statement for the loop, if True then enter for loop execution statement body;

3, then executes the expression 3;

4, return to the 2nd step cycle operation;

5. Jump out of the for statement until the loop ends.

Flow chart:

Instance:

The code is as follows Copy Code

for ($i =0; $i <=9; $i + +) {
$sum + = $i;
echo $sum. " T ";
}
?>


The Do...while Loop statement is a variant of the while loop, which functions like while, but checks that the expression is true after it executes the loop, and the basic structure is:

do{;
Statement body
}while (expression)
The Do...while Loop statement executes the statement body once, then evaluates the condition of the expression, returns the loop once if the value is true, and then jumps out of circulation for false.

Instance:

The code is as follows Copy Code

$i = 1;
$sum = 0;
do{
$sum + = $i;
echo $sum. " n ";
$i + +;
}while ($i <=10)
?>

While Loop statement
The while statement is a statement used by PHP to implement loops, with the following basic structure:

while (judgment statement) {
Execution statement body;
}
A judgment statement is usually judged by relational or logical operators.

The statement body is executed when the judgment statement is true, and then the value of the expression is checked, and if it is still true, the execution statement is executed again. Exits the loop until the judgment statement is false.

Instance:

The code is as follows Copy Code

$i = 0;
while ($i <9) {
$i + +;
echo $i. "
";
}
?>


The difference between while and Do...while:

The main difference between the two is that the first loop of the Do...while statement must be executed.

If the two loop bodies execute the same statements, they will run the same results, but the results of both loops will be different when the expression is false at first.

Instance:

The code is as follows Copy Code

/* While loop */
$a = 9;
while ($a >10) {
echo "enters the" while Loop statement body ";
}
/* Do...while Cycle */
do{
echo "enters Do...while loop statement body";
}while ($a >10)
?>


PHP exits the loop with the break and Continu statements, and their role is to jump out of the loop when the condition is met.

Break Statement usage:
When the value of the condition is true, the entire loop is terminated prematurely, and the statement outside the loop is executed.

Instance:

The code is as follows Copy Code

/* The output area is within 100 of the circular area */
for ($r = 1;; $r + +) {
$A =3.14* $r * $R;
if ($A >50) break; /* Form a dead loop if there is no break */
echo $A. "
";
}
?>

Continue statement usage:
The function of the continue statement is to end the loop and go to the next loop, not to exit the entire loop program.

Instance:

The code is as follows Copy Code

/* Output within 10 odd */
for ($i =1; $i <=10; $i + +) {
if ($i%2==0) continue;
echo $i. " T ";
}
?>

The difference between break and Continue statements:

As we can see from the above example, the effect of break and continue statements in exiting a loop is fundamentally different.

Continue just ends this cycle, and then returns to the loop body to continue the next cycle;

Break terminates the entire loop immediately and does not repeat.

http://www.bkjia.com/PHPjc/628843.html www.bkjia.com true http://www.bkjia.com/PHPjc/628843.html techarticle This article is to introduce the basic use of circular statements in PHP, including for Loop, while loop and do While loop use method, this article is very suitable for beginners PHP beginner Oh. ...

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