Right-Aligned Decreasing Triangle in PHP

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

What You’ll Learn

How to print a right-aligned decreasing number triangle in PHP. Each row begins at 5 and counts down to the row limit.

This pattern is excellent practice for combining alignment logic (spaces) with reverse loops (counting down).

⭐ Pattern Output

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

Output
    5
   5 4
  5 4 3
 5 4 3 2
5 4 3 2 1
1

Complete PHP Program

We print leading spaces for right alignment, then print numbers from $start down to the row limit.

PHP
<?php
$start = 5;

for ($i = $start; $i >= 1; $i--) {
    for ($s = 1; $s < $i; $s++) {
        echo " ";
    }
    for ($j = $start; $j >= $i; $j--) {
        echo $j . " ";
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Set the starting number

$start = 5; decides the top value and total rows.

Setup
2

Outer loop (row limit decreases)

for ($i = $start; $i >= 1; $i--) sets the minimum value printed on each row.

Row control
3

Print leading spaces

for ($s = 1; $s < $i; $s++) prints blanks so early rows appear shifted to the right.

Alignment
4

Print numbers from $start down to $i

for ($j = $start; $j >= $i; $j--) prints the decreasing sequence on the current row.

Number printing
=

Right-aligned decreasing triangle

Total printed values are 1+2+…+n = n(n+1)/2, so runtime is O(n²).

2

Variation — User Input (CLI) Version

Let the user decide the starting number at runtime using standard input (run from terminal with php):

PHP
<?php
echo "Enter the starting number: ";
$start = (int) trim(fgets(STDIN));

if ($start < 1) {
    exit("Starting number must be at least 1." . PHP_EOL);
}

for ($i = $start; $i >= 1; $i--) {
    for ($s = 1; $s < $i; $s++) {
        echo " ";
    }
    for ($j = $start; $j >= $i; $j--) {
        echo $j . " ";
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Use fixed-width formatting (like str_pad()) to align multi-digit numbers
  • Replace spaces with tabs or &nbsp; if printing in HTML
  • Change the start value to generate bigger triangles
  • Mirror the triangle to create a diamond-like pattern
  • Print commas instead of spaces for CSV-style output

Avoid

  • Using negative or zero start values without validation
  • Forgetting the newline after each row (PHP_EOL)
  • Mixing row and column responsibilities (outer loop = rows, inner loops = spaces/numbers)
  • Assuming user input is always valid

Key Takeaways

1

The triangle is right-aligned by printing leading spaces on each row.

2

Row $i prints the values $start, $start-1, ..., $i.

3

Total printed numbers are n(n+1)/2, so runtime is O(n²).

4

The same logic can be used to create other right-aligned increasing/decreasing patterns.

❓ Frequently Asked Questions

Because when $i = $start, the loop prints from $start down to $i, which is just one value.
Multi-digit numbers take more characters, so spaces are not enough for consistent columns. Use fixed-width formatting like str_pad().
Yes. Use <br> instead of PHP_EOL and consider printing &nbsp; for spaces, or use CSS grid for perfect alignment.
O(n²) for n rows because the program prints 1+2+…+n numbers.

Explore More PHP Number Patterns!

Practice alignment and reverse loops with more number patterns.

All Number Patterns →
Did you know?

This pattern is essentially a right-aligned triangle where each row prints a suffix of the sequence 5 4 3 2 1.

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