Home >Backend Development >PHP Problem >What is the usage of php do while
php do while is a loop statement that is guaranteed to be executed once. Its usage syntax is such as [fd630b5bddfea94b823000e498777068 0); ?>], its loop statement will be run exactly once.
do-while
(PHP 4, PHP 5, PHP 7)
do-while loop Very similar to a while loop, except that the value of the expression is checked at the end of each loop rather than at the beginning. The main difference from a normal while loop is that the do-while loop statement is guaranteed to be executed once (the truth value of the expression is checked after each loop), but in a normal while loop this is not necessarily the case (the truth value of the expression Checked at the beginning of the loop, if it is FALSE at the beginning, the entire loop terminates immediately).
There is only one syntax for do-while loops:
<?php $i = 0; do { echo $i; } while ($i > 0); ?>
The above loop will run exactly once, because after the first loop, when the truth value of the expression is checked, its value is FALSE ($i is not greater than 0) causing the loop to terminate.
Experienced C language users may be familiar with a different do-while loop usage, which puts the statement inside do-while(0) and uses a break statement inside the loop to end the execution loop. The following code snippet demonstrates this approach:
<?php do { if ($i < 5) { echo "i is not big enough"; break; } $i *= $factor; if ($i < $minimum_limit) { break; } echo "i is ok"; /* process i */ } while(0); ?>
Don’t worry if you don’t understand it right away. You can still write powerful code without using this "feature". Since PHP 5.3.0, you can also use goto to break out of loops.
The above is the detailed content of What is the usage of php do while. For more information, please follow other related articles on the PHP Chinese website!