PHP While Loop - Code

PHP Switch Statement Previous Next PHP Do While Loop


PHP Loops execute a block of code a specified number of times, or while specified condition is true.

PHP While: loops through a block of code while a specified condition is true.

Syntax


while(condition){ //code to be executed; }

Here is the code of php while loop.

Filename: php / while-loop.php


Code
<?php
$i=1;
while($i<=6){
    echo $i . "
"; $i++; } ?>

Running Step.


Type Url - https://localhost/php/while-loop.php

Output
1
2
3
4
5
6

PHP While Loop Without <br> Tag.

While loop without <br> tag will give output in same line.

Filename: php / while-loop1.php


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

Running Step.


Type Url - https://localhost/php/while-loop1.php

Output
123456

More Code Examples


PHP Decrease While Loop.

Decrease while loop will give output in descending order.

Filename: php / decrease-while-loop.php


Code
<?php
$i=6;
while($i>1){
    echo $i . "
"; $i--; } ?>

Running Step.


Type Url - https://localhost/php/decrease-while-loop.php

Output
6
5
4
3
2
1

PHP Decrease While Loop Without <br> Tag.

Decrease while loop without <br> tag will give output in descending order with same line.

Filename: php / decrease-while-loop1.php


Code
<?php
$i=6;
while($i>1){
    echo $i . "
"; $i--; } ?>

Running Step.


Type Url - https://localhost/php/decrease-while-loop1.php

Output
654321

Tags: PHP While Loop PHP Code Programming Language


Was this helpful ?