Reverse Ascending Number Triangle in PHP

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a reverse ascending number triangle in PHP using nested for loops. The triangle grows each row, but numbers are printed in reverse order on each line.

This pattern is excellent practice for writing a descending inner loop inside an ascending outer loop.

⭐ Pattern Output

For $n = 5, the pattern looks like this:

Output
1
21
321
4321
54321
1

Complete PHP Program

Fixed $n = 5 version:

PHP
<?php
$n = 5;

for ($i = 1; $i <= $n; $i++) {
    for ($j = $i; $j >= 1; $j--) {
        echo $j;
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Choose the size

$n = 5; sets how many rows are printed.

Setup
2

Outer loop (triangle grows)

for ($i = 1; $i <= $n; $i++) increases the row length by 1 each time.

Row control
3

Inner loop (print $i..1)

for ($j = $i; $j >= 1; $j--) prints the current row in reverse order, for example 4 prints 4321.

Number printing
4

New line

echo PHP_EOL; ends the row and moves to the next line.

Line break
=

Reverse ascending triangle

Total printed digits: 1+2+…+n = n(n+1)/2, so the time complexity is O(n²).

2

Variation — User Input (CLI) Version

Let the user decide $n at runtime (run from terminal with php):

PHP
<?php
echo "Enter the value of n: ";
$n = (int) trim(fgets(STDIN));

for ($i = 1; $i <= $n; $i++) {
    for ($j = $i; $j >= 1; $j--) {
        echo $j;
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Validate input (reject $n < 1) before printing
  • Add spaces between numbers (for example, echo $j . " ";)
  • Print the normal ascending triangle by counting $j up from 1 to $i
  • Right-align the triangle by printing leading spaces
  • Use the same technique for star or alphabet triangles

Avoid

  • Forgetting the newline after each row
  • Using negative or zero values without validating input
  • Mixing up loop directions (inner loop desc here)
  • Assuming CLI input is always valid (guard fgets() and casting if needed)

Key Takeaways

1

The outer loop grows the triangle from 1 row up to $n rows.

2

The inner loop prints the row in reverse: from $i down to 1.

3

Total printed digits follow n(n+1)/2, so complexity is O(n²).

4

This pattern is great practice for combining ascending and descending loops together.

❓ Frequently Asked Questions

Because when $i = 4, the inner loop starts at $j = 4 and decrements down to 1, printing 4, 3, 2, 1.
Set $n to the number of rows you want. For example, $n = 7 prints down to 7654321 on the last row.
Yes—use echo $j . " "; instead of echo $j;.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Next: PHP Number Pattern 8

Continue to Program 8 to practice another number pattern using nested loops in PHP.

Program 8 →
Did you know?

Patterns like this help you get comfortable switching loop directions. Many interview problems rely on the same idea: one loop grows while another counts down.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful