Starting PHP |
|
2024-12-25 |
|
|
13 |
|
|
While
<?php
$i = 0;
while($i++ < 10) {
echo $i . " ";
}
?>
Output
Do While
<?php
$i = 0;
do {
echo $i . " ";
} while($i++ < 10);
?>
Output
For
<?php
for($i=0; $i<10; $i++) {
echo $i . " ";
}
?>
Output
Break/Continue
<?php
for($i=0; $i<10; $i++) {
if($i==5) continue;
if($i==8) break;
echo $i . " ";
}
?>
Output
Note that break and continue can take a level argument that tells them how many
levels to break or continue out of.
Nested Loops
<?php
$done = false;
while(!$done) {
for($i=0; $i<10; $i++) {
if($i==8) break 2;
echo $i . " ";
}
}
?>
Output