Centered 1 to N Triangle in PHP

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

What You’ll Learn

How to print a centered triangle in PHP where each row prints numbers from 1 up to the row index.

We print a few leading spaces first, then print 1..$i with spacing between numbers.

⭐ Pattern Output

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

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

Complete PHP Program

The first inner loop prints leading spaces. The second inner loop prints numbers from 1 to $i.

PHP
<?php
$rows = 5;

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

🧠 How It Works

1

Set the row count

$rows = 5; sets the height of the triangle.

Setup
2

Outer loop (rows)

for ($i = 1; $i <= $rows; $i++) chooses which row we are printing.

Row control
3

Leading spaces

The loop from $rows down to $i+1 prints indentation to center the row.

Alignment
4

Print 1..$i

The loop for ($k = 1; $k <= $i; $k++) prints the ascending sequence.

Numbers
=

Centered triangle

Total printed numbers are 1+2+…+n = n(n+1)/2, giving 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);
}

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

💡 Tips for Enhancement

Try These

  • Use fixed-width formatting (str_pad()) for larger row counts
  • Print descending numbers to create a different centered triangle
  • Mirror the triangle to create a diamond
  • Replace digits with letters for an alphabet version
  • Remove the trailing space by building each row as a string then trimming it

Avoid

  • Using HTML entities like &nbsp; in CLI output
  • Forgetting to add a newline after each row
  • Assuming alignment stays perfect when values become multi-digit
  • Skipping input validation for user-provided row counts

Key Takeaways

1

Row $i prints 1..$i.

2

Leading spaces center the triangle shape.

3

Total printed numbers are n(n+1)/2.

4

Runtime is O(n²) for n rows.

❓ Frequently Asked Questions

Because on row $i=1, the loop 1..$i prints only one value.
Remove the first inner loop that prints spaces. Then the triangle becomes left-aligned.
Use <br> for line breaks and consider &nbsp; or CSS grid for consistent spacing.
O(n²) because the total printed values are 1+2+…+n.

Explore More PHP Number Patterns!

Try more centered triangles and pyramids to get comfortable with spacing logic.

All Number Patterns →
Did you know?

Most centered patterns are built by printing a shrinking number of leading spaces as the row index increases.

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