PHP Syntax Statements Looping do...while

 

- Info:

- do...while statement iterativly executes specified code as long as specified condition is TRUE.

- Unlike while statement, code is always executed at least once since condition is evaluated at the end of each iteration.

- condition is evaluated at the end of each iteration.

If it evaluates to TRUE code is executed.

If it evaluates to FALSE code is NOT execued and program breaks out of do...while loop.

 

- Syntax:

- do { code } while ( condition )

 

- Example:

- Create PHP Console Application Test.php.

 

Test.php

<?php

 

#SIMPLE. Output:12345.

$i = 0;

do{

$i++;

print $i;

} while($i<=4);

#BREAK. Output:12 since we break out of for loop when $i=3.

$i = 0;

do{

$i++;

if($i==3) { break; }

print $i;

} while($i<=4);

#CONTINUE. Output:1245 since display of number 3 is skipped.

$i = 0;

do{

$i++;

if($i==3) { continue; }

print $i;

} while($i<=4);

 

?>