PHP Basic Tutorial
The do...while
loop in PHP is similar to the while
loop but with one key difference: it executes the block of code at least once before evaluating the condition. This is useful when you want the code block to run at least once regardless of whether the condition is initially true
or false
.
do { // Code to be executed } while (condition);
do
block will always execute once before checking the while
condition.true
, the code block will execute again. If false
, the loop ends.Try it yourself
do...while
with a Condition that is Initially False$i
starts at 5, so the while
condition ($i < 5)
is false
.do
block runs once before checking the condition, printing "The value of i is: 5".Try it yourself