Reverse Repeating-Letter Alphabet Triangle in PHP

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

What You'll Learn

This program prints a triangle where each row repeats a single letter, but letters go backward: E, DD, CCC, BBBB, AAAAA.

It’s the mirror of Program 9 (A, BB, CCC, ...).

⭐ Pattern Output

For rows = 5:

Output
E
DD
CCC
BBBB
AAAAA
1

Complete PHP Program

Fixed rows = 5 version (nested loops):

PHP
<?php
$rows = 5;
$alpha = range('A', 'Z');

for ($i = $rows - 1; $i >= 0; $i--) {
    for ($j = $rows - 1; $j >= $i; $j--) {
        echo $alpha[$i];
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Alphabet array

$alpha = range('A', 'Z'); lets you access letters by index (0=>A, 1=>B, ...).

2

Outer loop walks backward

$i goes from $rows - 1 down to 0, selecting E, D, C, B, A for 5 rows.

3

Inner loop repeats the chosen letter

The inner loop runs from $rows - 1 down to $i, producing 1, 2, 3, 4, 5 repeats as $i decreases.

4

Print the line break

After building one row, print PHP_EOL to move to the next line. This keeps the output readable in CLI output.

Newline
=

Put it together

The outer loop controls rows, the inner loops control what prints on each row, and PHP_EOL separates the lines.

2

Variation — User Input (CLI) Version

Reads rows from standard input and clamps to a max of 26:

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

$alpha = range('A', 'Z');

for ($i = $rows - 1; $i >= 0; $i--) {
    for ($j = $rows - 1; $j >= $i; $j--) {
        echo $alpha[$i];
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Compare with Program 9 (forward letters)
  • Use str_repeat($alpha[$i], $rows - $i) to avoid the inner loop
  • Switch to lowercase using range('a', 'z')
  • Center-align by printing leading spaces

Avoid

  • Allowing $rows > 26 without handling the alphabet size
  • Using <br> in CLI examples (use PHP_EOL)
  • Changing the letter inside the inner loop if you want solid rows

Key Takeaways

1

The outer loop selects the row letter from E down to A (for 5 rows).

2

The inner loop repeats that letter, growing the row length.

3

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

❓ Frequently Asked Questions

When $i = 2 (0-based), the selected letter is $alpha[2] which is C. The inner loop runs three times, so you get CCC.
Choose $rows so that $rows - 1 maps to your top letter (e.g., 4 rows start from D).

Next: PHP Alphabet Pattern 11

Continue to the next program for another alphabet pattern in PHP.

Program 11 →
Did you know?

You can compute the row letter without an array: chr(ord('A') + $i). For reverse order you can use chr(ord('A') + ($rows - 1 - $i)).

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.

10 people found this page helpful