Home > Backend Development > PHP Tutorial > Detailed explanation of 'do...while' loop statement examples of PHP loop control statements

Detailed explanation of 'do...while' loop statement examples of PHP loop control statements

怪我咯
Release: 2023-03-07 13:26:02
Original
3009 people have browsed it

I explained to you the while statement loop in the PHP loop statement. In fact, the while statement has another form of expression, which is what we are going to explain to you today, "do...while" loop Statement .

The concept of do...while loop statement

do...while loop statement is very similar to while loop statement. The difference between the two is that, The do...while loop statement loops one more time than the while loop statement. The while loop statement will jump out of the current loop directly when the expression is false, while the do...while loop statement will be executed once PHP statements are used to judge conditional expressions. Just like, when we usually go to the water dispenser to get water, there are two types of people. One type will first look to see if there is water in the bucket. If there is, then press the button to get the water. This is the while loop ; The other kind of people press the button first regardless of whether there is water or not. When the water comes out, they directly receive the water. If no water comes out, they check to see if there is water in the bucket, and then leave silently. This is a do...while loop.

The syntax of do...while loop statement

do {
    statement
} while (expr);
Copy after login

Detailed syntax explanation:

See from the syntax , our conditional expression is placed after the PHP statement statement, which means that no matter whether the expr expression is true or not, the do...while loop will be executed at least once.

Now let’s look at the flow chart of our

do...while loop statement

Detailed explanation of do...while loop statement examples of PHP loop control statements

do...while loop statement Example

This example compares the difference between do...while loop and while loop by running two statements. The code is as follows

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码

$num=1;                             //声明一个整型变量$sum
while($num!=1){                     //使用while循环输出
    echo "不会看到";                //这句话不会输出
} 
do{                                  //使用do...while循环输出
    echo "会看到";                   //这句话会输出
}while($num!=1);

?>
Copy after login

The result of running the code:

Detailed explanation of do...while loop statement examples of PHP loop control statements

As you can see from the above example, our conditional expression is false and the while loop will not output, and the do...while loop will be executed once regardless of whether the expression is false.


In the next section, we will explain in detail the "for" loop in the PHP loop statement.


The above is the detailed content of Detailed explanation of 'do...while' loop statement examples of PHP loop control statements. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template