DEV Community

Cover image for PHP While Loop Statements
Javasper
Javasper

Posted on • Updated on

PHP While Loop Statements

A while loop executes a certain block of code until a particular condition is no longer true.
Syntax

<?php
while(conditional)
{ 
//code block
}
?>
Enter fullscreen mode Exit fullscreen mode

A while loop says as long as this condition inside the brackets () is true, keep executing the code block inside of {}.
Example

<?php
$i=0;
while($i<10)
{
echo $i;
echo "<br>";
echo $i++;
}
?>
Enter fullscreen mode Exit fullscreen mode

Result

1.png
Your while loop should have inside it 1) a counter and 2) a way of increasing the counter.
Otherwise, your loop will keep repeating forever without a way to end.

Discussion (0)