Skip to main content

Command Palette

Search for a command to run...

PHP While Loop Statements

Updated
•1 min read•View as Markdown
PHP While Loop Statements
E

I'm a full-stack web developer who loves building new projects and learning new things

It might not show from my posts, but I'm also quite funny.

Colin Viebrock, CC BY-SA 4.0, via Wikimedia Commons

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

<?php
while(conditional)
{ 
//code block
}
?>

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++;
}
?>

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.

More from this blog

Javasper

50 posts

I'm a full stack software developer who loves to building new projects and solving difficult problems.

It might not be obvious from my posts, but I'm actually quite funny.