PHP While Loops
Loops are used to execute a code till a specific condition/counter.
PHP Supports four type of Loops
- For Loop
- While Loop
- Do…While Loop
- Foreach Loop
While Loop
While Loop executes the code of block until it’s specified condition comes true.
<?php
$counter=1;
while($counter<=5){
echo $counter."<br>";
$counter++;
}
?>
While Loop with Break
Break is used to terminate the while loop during block execution.
<?php
$counter=1;
while($counter<=5){
echo $counter."<br>";
if($counter==3){
echo "Break at 3 <br>";
break;
}
$counter++;
}
?>
Do…While Loop
Do…While Loop works same like while loop but once it will run it’s code block however condition is true or false.
<?php
$counter=1;
do{
echo $counter."<br>";
$counter++;
}while($counter<=5);
?>