How to make the inverted number pyramid using PHP
In this tutorial, we will discuss How to make the inverted number pyramid using PHP. I will let you know the simple process of the inverted number pyramid using PHP with for loop. PHP for loop use execute a block of code for a specified number of times. Using for or for each loop to make an inverted number pyramid is much easier. I am using for loop to inverted number pyramid using PHP because it is easier to understand that how many rows I have to iterate. In this example, we will discuss a 9 to 1 inverted number display. first for loop in $i = 9
,$i>=1
and $i--
to inverted number.
Example
<?php for($i=9; $i>=1; $i--) { echo $i."<br>"; } Output: 9 8 7 6 5 4 3 2 1
Example
<?php for($i=9; $i>=1; $i--) { for($k=$i; $k>=1; $k--){ echo " ".$i." "; } echo "<br>"; } Output: 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
inverted number pyramid
<?php for($i=9; $i>=1; $i--) { for($j=0; $j<= 9-$i; $j++){ echo " "; } for($k=$i; $k>=1; $k--){ echo " ".$i." "; } echo "<br>"; } Output: 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
Draw number pyramid in PHP
<?php for($i=1; $i<=9; $i++) { for($j=0; $j<= 9-$i; $j++){ echo " "; } for($k=$i; $k>=1; $k--){ echo " ".$i." "; } echo "<br>"; } Output: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9