The difference ...LOGIN

The difference between do...while loops in PHP process control

do...while has basically the same syntax structure as while. It is also a Boolean loop and its functions are basically the same.

The basic syntax is as follows:

do {
   //代码块
} while (判断);

do...The difference between while and while is that their values ​​are checked at different times.

do-while Regardless of whether the while judgment is true, the code block loop statement is executed once, and it is guaranteed to be executed once (the truth value of the expression is checked after each loop).
However, our previous while loop will check the Boolean judgment area and execute it if it is true. If it is not established, it will not be executed.

Let’s use the code to verify it:

<?php
$i = 0;
do {
   echo $i;
} while ($i > 0);
?>

In the above code, $i is definitely not greater than 0, and it is also executed.


Of course, if you don’t understand it yet, it doesn’t matter if you really can’t think of the application scenario. You can skip this block completely.

do...while is rarely used. We may use it in resource processing such as file opening, etc.


Next Section
<?php $i = 0; do { echo $i; } while ($i > 0); ?>
submitReset Code
ChapterCourseware