nested loop is a loop within a loop, an inner loop within the body of an outer one.
//nested for
for (expr1; expr2; expr3) {
for (expr1; expr2; expr3) {
//statement;
}
}
Normally nested for loop is used. while and do.. while not used.
<?php
for ($i = 1; $i < 5; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo " * ";
}
echo '<br />';
}
?>
<?php
for ($i = 1; $i < 5; $i++) {
for ($j = $i; $j < 5; $j++) {
echo " * ";
}
echo '<br />';
}
?>
<?php
for ($i = 1; $i < 5; $i++) {
for ($j = $i; $j <= 5; $j++) {
echo " "; // it will print blank space
}
for ($j = 1; $j <= $i; $j++) {
echo " * ";
}
echo '<br />';
}
?>
Hi dude, i have also find out one good example Loops – PHP Example
Ask Question