PHP while loop

While loop is another powerful function available in PHP that helps to reduce down the effort and time consumption. 

PHP While Loop

I have already discussed about for loop in php

We are going to follow same logic from for loop in while loop also. 

Lets print 1 to 10 using while loop. 

Here the starting value is 1, each time the value increments by 1 and the final value is 10.

Syntax

Starting Value
While (condition){
    // task
increment or decrement value
}
Lets print 1 to 10 using while loop

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

Output
1
2
3
4
5
6
7
8
9
10

Assignment

Print 10 to 1 in reverse order using while loop

Solution

<?php
$i=10;
while($i>=1){
echo $i;
$i--;
}
?> 

Output:
10
9
8
7
6
5
4
3
2
1
PHP while loop PHP while loop Reviewed by Pixel Stream on December 10, 2019 Rating: 5

No comments:

Powered by Blogger.