Fixed Start 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 fixed start descending number pattern in PHP using nested for loops. Each row always starts from $n and counts down, but the row gets shorter on each line.

This pattern is a simple way to practice controlling the end condition of the inner loop based on the outer loop variable.

⭐ Pattern Output

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

Output
54321
5432
543
54
5
1

Complete PHP Program

Fixed $n = 5 version:

PHP
<?php
$n = 5;

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

🧠 How It Works

1

Choose the size

$n = 5; sets the starting digit (printed first on every row).

Setup
2

Outer loop (row index)

for ($i = 1; $i <= $n; $i++) controls how many digits are removed from the end on each next row.

Row control
3

Inner loop (print $n..$i)

for ($j = $n; $j >= $i; $j--) always starts at $n, counts down, and stops at the current row index $i.

Number printing
4

New line

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

Line break
=

Fixed-start descending pattern

You print n + (n-1) + ... + 1 digits total, which is 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 = $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 . " ";)
  • Flip the direction to print ascending patterns (start from 1 and count up)
  • Right-align the pattern by printing leading spaces before each row
  • Try the same logic using letters instead of digits

Avoid

  • Forgetting the newline after each row
  • Using negative or zero values without validating input
  • Mixing up which variable controls the row length
  • Assuming CLI input is always valid (guard fgets() and casting if needed)

Key Takeaways

1

The outer loop increases the row index $i from 1 to $n.

2

The inner loop always starts at $n and stops at $i, making rows shorter each time.

3

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

4

This technique is useful for inverted patterns where the starting point stays constant.

❓ Frequently Asked Questions

Because the inner loop always begins with $j = $n. Only the stop value changes (it stops at $i), so the first digit stays constant.
Set $n to the size you want. For example, $n = 7 prints 7654321, then 765432, down to 7.
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 5

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

Program 5 →
Did you know?

By keeping the start fixed at $n, you can generate many “trim from the end” patterns just by changing the inner loop’s stop condition.

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