PHP Programing language

adplus-dvertising
PHP DOWHILE-LOOP
Previous Home Next

A "do while" loop is a slightly modified version of the while loop. In Do while loop if the conditional statement is false then the code within the loop is not executed.

On the other hand, At least one time do-while loop for block of code .because the conditional statement is not checked until after the contained code has been executed.

PHP While-Loop and DoWhile-Loop Contrast

Example that illustrates the difference between these two loop types is a conditional statement that is always false.

The while loop:

PHP Code:

<?php
$a = 0;
while($a > 1){
	echo "hello r4rtechsoft";
} 
?>

Output:

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?

The do-while loop

PHP Code:
<?php
$a= 0;
do {
	echo "hello r4rtechsoft";
} while ($a > 1);
?>

Output:

The code segment "hello r4rtechsoft" 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.

Previous Home Next