Bidirectional Number Triangle in PHP

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

What You’ll Learn

How to print a repeating-number decreasing triangle in PHP:

11111, 2222, 333, 22, 1.

We use a decreasing inner loop for row length and a small conditional to mirror values on the last rows.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
11111
2222
333
22
1
1

Complete PHP Program

The inner loop controls row length; the printed digit depends on whether we are in the first half or mirrored half.

PHP
<?php
$n = 5;

for ($i = 1; $i <= $n; $i++) {
    for ($j = $i; $j <= $n; $j++) {
        if ($i < 4) {
            echo $i;
        } else {
            echo 6 - $i;
        }
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Choose n

$n = 5; controls the triangle height and first row width.

Setup
2

Outer loop (rows)

for ($i = 1; $i <= $n; $i++) increments the row number.

Row control
3

Inner loop (decreasing width)

for ($j = $i; $j <= $n; $j++) prints fewer characters each row as $i increases.

Width
4

Choose which digit to print

We print $i for rows 1..3, then mirror using 6 - $i for rows 4..5 to produce 2 and 1.

Mirroring
=

Repeating decreasing triangle

Row width decreases by one each line, creating a compact triangle.

2

Variation — User Input (CLI) Version

Let the user choose the number of rows (run from terminal with php):

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

if ($n < 1) {
    echo "Please enter a positive number." . PHP_EOL;
    exit(1);
}

for ($i = 1; $i <= $n; $i++) {
    for ($j = $i; $j <= $n; $j++) {
        $val = ($i <= (int) ceil($n / 2)) ? $i : (($n + 1) - $i);
        echo $val;
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Replace the digit with a character to create symbol triangles
  • Print spaces between digits for larger $n values
  • Use a single formula for the value (as shown in the CLI version)
  • Mirror horizontally by padding with spaces on the left
  • Convert it into an increasing triangle by changing the inner loop bounds

Avoid

  • Hardcoding the mirroring constants if you plan to scale $n
  • Forgetting that the inner loop controls the decreasing width
  • Skipping validation for CLI input
  • Assuming the pattern is symmetric for any n (check even n behavior)

Key Takeaways

1

The inner loop prints from $i to $n, shrinking each row.

2

The printed digit can be computed from the row index.

3

For fixed n=5, the mirror shortcut 6 - $i works for the last rows.

4

Generalize by deriving the mirrored value from $n instead of hardcoding.

❓ Frequently Asked Questions

Because the inner loop starts at $j = $i and runs to $n, so each next row prints one fewer value.
For n=5, it maps 4 → 2 and 5 → 1, creating the mirrored values for the last rows.
Yes, but use the generalized formula shown in the CLI version instead of hardcoding 6 - $i.
O(n²) for n rows, because total prints are \(1+2+\dots+n\).

Explore More PHP Number Patterns!

Repeating-number triangles are perfect practice for nested loops and conditional logic.

All Number Patterns →
Did you know?

Many pattern problems can be simplified by finding a formula for the printed value per row—once you have that, the rest is just loop bounds.

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