Do while Loop in PHP

Do while Loop in PHP

The do while loop executes a block of statements once, it then checks the condition and repeats the loop as long as the specified condition is true. In doing while loop the condition is checked after executing a statement. It means that when its first time of the loop,then do while loop will execute its statement at least once, even if the condition is false.

Flowchart of Do While Loop

do while loop in php
Figure: do while loop in PHP.

Example of Do while loop.

An example program to demonstrate the Do while loop.

Output

value is: 7

Code in PHP

Explanation

<?phpPHP opening.
  $x=7;Variable is  initialized with value 7.
 dodo is the keyword
 {Starting the loop.
                 echo “value is :$x<br>”;Statement within loop that will executes and print the value.
                 $x++;Value of variable is incremented  by 1 and loop will repeats the execution of statements until the condition remains true.
 }Ending of loop
while($x<=6)Here the condition is checked 
?>Closing tag.

Comparison of do While Loop in PHP VS C++

Code in PHPSame code in C++
<?php
  $x=7;int x=7;
 dodo
 {
                 echo “value is :$x<br>”;cout <<”value is :”<<x<<endl;
                 $x++;x++;
 }
while($x<=6)while(x<=6)
?>

Examples of do While loop in PHP

Subscribe for Friendship

Latest posts by Prof. Fazal Rehman Shamil (see all)

Buy advertisement space on T4Tutorials

For more details email [email protected]