Inverted Repeating-Letter Alphabet Triangle in PHP

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

What You'll Learn

This PHP program prints an inverted repeating-letter triangle: EEEEE, DDDD, CCC, BB, A.

It complements Program 10 (growing width) and Program 9 (forward letters).

⭐ Pattern Output

For rows = 5:

Output
EEEEE
DDDD
CCC
BB
A
1

Complete PHP Program

Fixed rows = 5 version:

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

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

🧠 How It Works

1

Pick the row letter

The outer loop starts at $rows - 1 (E for 5 rows) and moves backward to 0 (A).

2

Repeat count shrinks

The inner loop runs from 0 to $i, so it prints $i + 1 times: 5, 4, 3, 2, 1.

3

One letter per row

Because the code prints $alpha[$i] and $i doesn’t change inside the inner loop, each row is a solid block of one letter.

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 = 0; $j <= $i; $j++) {
        echo $alpha[$i];
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Compare with Program 10 (growing width)
  • Use str_repeat($alpha[$i], $i + 1) to avoid the inner loop
  • Switch to lowercase using range('a', 'z')
  • Add spaces between letters (e.g., $alpha[$i] . ' ')

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

Outer loop selects the row letter in reverse: E, D, C, B, A.

2

Inner loop prints a shrinking number of repeats: 5, 4, 3, 2, 1.

3

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

❓ Frequently Asked Questions

It creates an array of uppercase letters so you can print alphabets using simple numeric indexes (0 for A, 1 for B, ...).
PHP_EOL is correct for CLI output and keeps examples consistent for terminal execution. In HTML, you would use <br> instead.
Most pattern programs print O(n) characters per row for n rows, so they are typically O(n2).

Next: PHP Alphabet Pattern 12

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

Program 12 →

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