Home > Article > Backend Development > PHP process control do-while
This article mainly introduces the do-while of PHP process control, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
This article is for basic useLearners, experts please close this page
Read this article for 3 minutes, it’s hard to understand those who have difficulty?
(PHP 4 , PHP 5, PHP 7)
The do-while loop is very similar to the while loop, except that the value of the expression is checked at the end of each loop instead of 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), however in a normal while loop
is not necessarily true (the truth value of the expression is 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
(not greater than 0) causes the loop to terminate.
Experienced C language users may be familiar with a different do-while loop usage, which is to put the statement inside do-while(0) and use the break statement inside the loop. End the execution loop. The following code snippet demonstrates this method:
<?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); ?>
Related recommendations:
php process control process control Alternative syntax for
The above is the detailed content of PHP process control do-while. For more information, please follow other related articles on the PHP Chinese website!