Saturday, May 3, 2014

PHP while and do while Loop

The PHP while Loop

The while loop executes a block of code as long as the specified condition is true.

Syntax

while (condition is true) {
  code to be executed;
}
The example below first sets a variable $x to 1 ($x=1;). Then, the while loop will continue to run as long as $x is less than, or equal to 8. $x will increase by 1 each time the loop runs ($x++;):

Example

<?php
$x=1;

while($x<=8) {
  echo "The number is: $x <br>";
  $x++;
}
?> 
 Ouptput:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5  
The number is: 6
The number is: 7
The number is: 8

The PHP do...while Loop

The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

Syntax

do {
  code to be executed;
} while (condition is true);
The example below first sets a variable $x to 1 ($x=1;). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5:

Example

<?php
$x=1;

do {
  echo "The number is: $x <br>";
  $x++;
} while ($x<=5);
?>
Output:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5 

No comments:

Post a Comment