Home >Backend Development >PHP Tutorial >Detailed explanation of 'do...while' loop statement examples of PHP loop control statements
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);
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 ourdo...while loop statement
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); ?>The result of running the code: 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.
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!