Free Guides
Language Tutorials

PHP: Hypertext Preprocessor
PHP DO WHILE
On the other hand, a do-while loop always executes its block of code at least once. This is because the conditional statement is not checked until after the contained code has been executed.
PHP - While Loop and Do While Loop Contrast
A simple example that illustrates the difference between these two loop types is a conditional statement that is always false. First the while loop:
PHP Code:
$cookies = 0;
while($cookies > 1){
echo "i love itsyllabus.com*";
}
Display:
As you can see, this while loop's conditional statement failed (0 is not greater than 1), which means the code within the while loop was not executed. Now, can you guess what will happen with a do-while loop?
PHP Code:
$cookies = 0;
do {
echo "i love itsyllabus.com*";
} while ($cookies > 1);
Display:
The code segment "i love itsyllabus.com!" was executed even though the conditional statement was false. This is because a do-while loop first do's and secondly checks the while condition!
Chances are you will not need to use a do while loop in most of your PHP programming, but it is good to know it's there!