Growing Prefix Descending Number Pattern in PHP

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

What You’ll Learn

How to print a growing prefix descending number pattern in PHP using nested for loops. The first row prints the last number only, and each next row adds the next descending digit.

This pattern is a good way to practice descending inner loops where the stop condition depends on the outer loop.

⭐ Pattern Output

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

Output
5
54
543
5432
54321
1

Complete PHP Program

Fixed $n = 5 version:

PHP
<?php
$n = 5;

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

🧠 How It Works

1

Choose the size

$n = 5; sets the largest digit printed on every row.

Setup
2

Outer loop (grow row length)

for ($i = $n; $i >= 1; $i--) decreases $i, which causes the inner loop to run more times each next row.

Row control
3

Inner loop (print $n..$i)

for ($j = $n; $j >= $i; $j--) prints a descending sequence starting at $n and stopping at $i.

Number printing
4

New line

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

Line break
=

Growing prefix pattern

Total printed digits still form a triangular count, 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 = $n; $i >= 1; $i--) {
    for ($j = $n; $j >= $i; $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 shrinking version by reversing the outer loop direction
  • Right-align the triangle by printing leading spaces
  • Try the same pattern with letters to create alphabet sequences

Avoid

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

Key Takeaways

1

The outer loop controls how many digits are printed per row by changing the stop value.

2

The inner loop prints from $n down to $i on each row.

3

Total printed digits grow like a triangular number, so time is O(n²).

4

This is a useful building block for many reversed and mirrored patterns.

❓ Frequently Asked Questions

Because the outer loop starts at $i = $n. The inner loop then prints from $j = $n down to $i, which is only one digit when $i = $n.
Set $n to the size you want. For example, $n = 7 prints 7, then 76, then 765, up to 7654321.
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 9

Continue to Program 9 to learn a repeated-digit number pattern in PHP.

Program 9 →
Did you know?

Patterns like this are great practice for controlling loop boundaries. Small changes to the inner loop range can create completely different pattern families.

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