Sunday, August 20, 2017

PHP While Loop


while loop is the simplest type of loop in PHP and it is quite similar to for loop. we can write the while loops in two way. Lets see the Syntax for while loop.

Syntax :
while(condition){  
//code to be executed here 
}

The meaning of while loop is simple. When the condition is true it will go inside the loop otherwise it will skip the loop.

Alternative Syntax:
while(condition):  
//code to be executed here 
  
endwhile;

Example : 1
<?php

$i = 1;
while ($i <= 10) {
    echo $i."<br>";  
    $i++;
} ?>

Output :
----------------------------
1
2
3
4
5
6
7
8
9
10

Example :2
<?php

$i = 1;
while ($i <= 10):
    echo $i."<br>";
    $i++;
endwhile;
?>

Output:
--------------------
1
2
3
4
5
6
7
8
9
10



No comments:

Post a Comment