PHP while loop

      No Comments on PHP while loop

PHP while loop execute a block of code while the specified condition is true.

Syntax:

while (condition is true)
{
    code to be executed;
} 

Example:

 <?php
$w = 1;
while($w <= 5)
{
    echo "The number is: $x <br>";
    $w++;
}
?> 

Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

Explanation:
Variable $w is set to 1. The condition is checked , and the while loop executes till $x is less than 5.The variable $w is incremented by 1, every time the loop runs.

Another Example:

<?php 
$x=1;
while($x<=10)
{
echo "2 *".$x."=".2*$x;
$x++;
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *