saftutorials.com
PHP while loop Tutorial

Loops are used to execute the part of code multiple times or a specified time in PHP. These all are the loops which are supported by PHP language.


How many loops in PHP?
While Loop, Do white loop, For loop, Foreach Loop.

Syntax of while loop

while (condition) {

the code will be executed till condition will be true;

}


Example of while loop

Initially, $a variable is initialized by 1 and $b by 10.The while loop starts to execute the code as long as $a is less than $b and $a will increase by 1 every time loops run.

<!DOCTYPE html>
<html>
<body>
<?php
$a = 1;
$b = 10;
?>
<?php
while($a < $b) {
?>

<p><?php echo $a." Time"; ?>  : <?php echo $a . ' is less than ' .$b;?></p>
<?php
$a++;
}
?>
</body>
</html>


The above code produces the following results

Outputs:

1 Time: 1 is less than 10

2 Time: 2 is less than 10

3 Time: 3 is less than 10

4 Time: 4 is less than 10

5 Time: 5 is less than 10

6 Time: 6 is less than 10

7 Time: 7 is less than 10

8 Time: 8 is less than 10

9 Time: 9 is less than 10


Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic