PHP Syntax Statements Looping while

 

- Info:

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

- condition is evaluated at the begining 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 while loop.

 

- Syntax:

- while (condition) { code }

 

- Example:

- Create PHP Console Application Test.php.

 

Test.php

<?php

 

#SIMPLE. Output:12345.

$i = 0;

while($i<=4) {

$i++;

print($i);

}

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

$i = 0;

while($i<=4){

$i++;

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

print($i);

}

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

$i = 0;

while($i<=4){

$i++;

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

print($i);

}

 

?>