How to make the inverted number pyramid using PHP
In this tutorial, we will learn how to make an inverted number pyramid using PHP. We will use simple for loops to print different number patterns, including:
- A basic countdown from 9 to 1
- A left-aligned inverted number triangle
- A centered inverted number pyramid
- A normal (upright) number pyramid
- By the end of this tutorial, you will clearly understand how nested loops work in PHP to generate number patterns.
Understanding the PHP for Loop
The PHP for loop is used to execute a block of code a specific number of times. Its basic syntax looks like this:
for (initialization; condition; increment/decrement) {
// code to execute
}
In our examples, we will mainly use:
- A loop variable (like
$i) - A starting value
- A condition (
>=or<=) - Increment or decrement (
++or--)
Example 1: Simple Countdown from 9 to 1
First, let us display numbers from 9 down to 1 using a single for loop.
<?php
for ($i = 9; $i >= 1; $i--) {
echo $i . "<br>";
}
Output:
9
8
7
6
5
4
3
2
1
Here:
$i = 9is the starting value$i >= 1is the condition$i--decreases the value of$ion each iteration
Example 2: Left-Aligned Inverted Number Triangle
Now, let us build a left-aligned inverted number triangle.
Each row will display the same number repeated, and the number of repetitions will decrease on each row.
<?php
for ($i = 9; $i >= 1; $i--) {
for ($k = $i; $k >= 1; $k--) {
echo " " . $i . " ";
}
echo "<br>";
}
Output (visual representation):
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
How it works:
- Outer loop (
$i) controls the number and the number of rows. - Inner loop (
$k) controls how many times the number is printed in each row.
Example 3: Inverted Number Pyramid (Centered)
Now, we will create a centered inverted number pyramid.
We will use two nested for loops inside the main loop:
- One inner loop to print spaces
- Another inner loop to print the numbers
<?php
for ($i = 9; $i >= 1; $i--) {
// Print leading spaces
for ($j = 0; $j <= 9 - $i; $j++) {
echo " ";
}
// Print numbers
for ($k = $i; $k >= 1; $k--) {
echo " " . $i . " ";
}
echo "<br>";
}
Output (visual representation):
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
Explanation:
- The first inner loop (
$j) adds spaces before the numbers to center the pyramid. - The second inner loop (
$k) prints the actual numbers in each row. - As
$idecreases, the number of spaces increases, so the pyramid shifts to the right.
Example 4: Normal Number Pyramid (Bottom to Top)
To complete the tutorial, let us reverse the logic and print a normal (upright) number pyramid from 1 to 9.
<?php
for ($i = 1; $i <= 9; $i++) {
// Print leading spaces
for ($j = 0; $j <= 9 - $i; $j++) {
echo " ";
}
// Print numbers
for ($k = $i; $k >= 1; $k--) {
echo " " . $i . " ";
}
echo "<br>";
}
Output (visual representation):
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
Conclusion
Creating number patterns like inverted pyramids in PHP is a great way to understand nested loops and how they work together. In this tutorial, we covered:
- A simple countdown from 9 to 1
- A left-aligned inverted number triangle
- A centered inverted number pyramid
- A normal number pyramid
You can change the starting and ending values (like using 5 instead of 9) or modify spacing to experiment with different patterns.