Right-Aligned Number Triangle in PHP

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

What You’ll Learn

How to print a right-aligned number triangle in PHP where numbers increase continuously (1, 2, 3, …) across rows.

You’ll use a counter variable and nested loops to print spaces on the left and numbers on the right.

⭐ Pattern Output

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

Output
    1
   2 3
  4 5 6
 7 8 9 10
11 12 13 14 15
1

Complete PHP Program

We print blanks for alignment, then print numbers using a counter $k that increments after each print.

PHP
<?php
$rows = 5;
$k = 1;

for ($i = 1; $i <= $rows; $i++) {
    // leading blanks (as spaces) for right alignment
    for ($s = $rows - $i; $s >= 1; $s--) {
        echo " ";
    }

    for ($j = 1; $j <= $i; $j++) {
        echo $k++ . " ";
    }

    echo PHP_EOL;
}

🧠 How It Works

1

Initialize rows and counter

$rows = 5; sets the height and $k = 1; starts the sequence.

Setup
2

Outer loop (rows)

for ($i = 1; $i <= $rows; $i++) prints one row at a time.

Row control
3

Print leading spaces

The loop from $rows - $i down to 1 prints blanks so the numbers shift to the right.

Alignment
4

Print sequential numbers

The inner loop runs $j = 1..$i and prints $k++ each time.

Number printing
=

Right-aligned triangle

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

2

Variation — User Input (CLI) Version

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

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

if ($rows < 1) {
    exit("Rows must be at least 1." . PHP_EOL);
}

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

💡 Tips for Enhancement

Try These

  • Format spacing using str_pad() to align multi-digit numbers (10, 11, 12, ...)
  • Reset the counter each row to print repeated sequences per line
  • Convert it into a centered pyramid by doubling the left padding
  • Print the same triangle in reverse (descending) order
  • Use HTML output with CSS grid when rendering on a webpage

Avoid

  • Forgetting to reset $k when you want a fresh sequence for a new run
  • Using raw spaces for large patterns with multi-digit numbers (alignment will drift)
  • Mixing row and column responsibilities (outer loop = rows, inner loops = spaces/numbers)
  • Skipping input validation in the CLI version

Key Takeaways

1

A counter ($k) prints a continuous sequence across rows.

2

Leading spaces right-align the triangle (spaces = $rows - $i).

3

Row $i prints $i numbers, so total prints are n(n+1)/2.

4

Runtime grows as O(n²) with the number of rows.

❓ Frequently Asked Questions

Because 10 takes two characters while 9 takes one. Use fixed-width formatting (like str_pad()) to keep columns aligned.
Replace PHP_EOL with <br> and use &nbsp; for spacing, or render with HTML/CSS (grid/flex) for better alignment.
Yes. Remove the leading-space loop, and just print the numbers for each row.
O(n²) for n rows because the program prints 1+2+…+n numbers.

Explore More PHP Number Patterns!

From simple triangles to advanced alignments—keep practicing nested loops.

All Number Patterns →
Did you know?

The count of numbers printed after n rows is a triangular number: 1+2+…+n = n(n+1)/2. For 5 rows, that’s 15.

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