In front of you to explain the PHP loop statement in the while statement loop, in fact, while the statement has another form of expression, which is what we are going to explain to you today,
"Do...while" loop statement。
Do...while The concept of cyclic statements
Do...while loop Statements And while loop statements are very similar, the difference between the two is that the Do...while Loop statement is more than a while loop, while the loop statement, when the expression is false, will directly jump out of the current loop, and do ... The While Loop statement executes the PHP statement first, judging the conditional expression. Like, we usually go to the water dispenser, there are two kinds of people, a person will first look at the bucket there is no water, if there is, then press the button to connect the water, this is the while loop ; another person whether there is no water, the first to press the button, water is directly connected, if there is no effluent, Again to see if there is water in the bucket, and then silently leave, this is the do...while cycle.
Do...while syntax for a looping statement
Do { statement} while (expr);
Syntax Explanation:
As you can see from the syntax, our conditional expression is placed behind the PHP statement statement, which means that no matter whether the expr expression is true,do...while, the loop executes at least once.
Now, let's do...while the flowchart of the loop statement.
Do...while Loop Statement Instance
This example compares the difference between a do...while loop and a while loop by running two statements. The code is as follows
<?phpheader ("Content-type:text/html;charset=utf-8"); Set the encoding $num=1; Declares an integer variable $sumwhile ($num!=1) { //using the while loop output echo "will not see"; This statement will not output} do{ //Use do...while loop output echo "will see"; This sentence will output}while ($num!=1); >
Code Run Result:
As can be seen from the above example, our conditional expression is false, while the loop is not output, and the Do...while loop executes once regardless of whether the expression is false.
In the next section, we will explain the "for" loop in the PHP looping statement.