The PHP for loop is used to execute the same part of code a given number of times. Using for loop we can make the different types of number tringle. we can make the looping logic, to execute the same block of code a specified number of times.
For fresher it’s good practice to learn the programming syntax and to improve the logic. Few numerical triangle programming syntax with output as below.
<?php
for($i=0;$i<=9;$i++){
for ($d=10-$i; $d > 0; $d--) {
echo " ";
}
for($j=1;$j<=$i;$j++){
echo " ".$i." ";
}
echo "<br>";
} ?>
|
|
<?php
for($i=9;$i>=1;$i--){
for ($d=0; $d <= 9-$i; $d++) {
echo " ";
}
for($j=$i;$j>=1;$j--){
echo " ".$i." ";
}
echo "<br>";
}
?>
|
|
<?php
for($i=0;$i<=9;$i++){
for ($d=10-$i; $d > 0; $d--) {
echo " ";
}
for($j=1;$j<=$i;$j++){
echo " ".$i." ";
}
echo "<br>";
}
for($i=8;$i>=1;$i--){
for ($d=0; $d <= 9-$i; $d++) {
echo " ";
}
for($j=$i;$j>=1;$j--){
echo " ".$i." ";
}
echo "<br>";
}
?>
|
|