Reverse Alphabet Pattern (EDCBA to E) in PHP

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

What You'll Learn

In this pattern, every row begins with the same letter (E for 5 rows). Each next row gets shorter by dropping the last character.

Output for rows = 5: EDCBA, EDCB, EDC, ED, E.

⭐ Pattern Output

Output
EDCBA
EDCB
EDC
ED
E
1

Complete PHP Program

Fixed rows = 5 version:

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

$end = $rows - 1; // 4 -> 'E'

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

🧠 How It Works

1

Build the alphabet array

range('A','Z') creates ['A','B','C',...,'Z'].

2

Fix the first letter

$end is constant. The inner loop always starts at $end, so every row starts with E (for 5 rows).

3

Shorten rows by raising the stop

As $i increases, the inner loop stops sooner ($j >= $i), producing 5, 4, 3, 2, 1 characters.

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');
$end = $rows - 1;

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

💡 Tips for Enhancement

Try These

  • Compare with Program 7 (moving first letter each row)
  • Use lowercase letters with range('a','z')
  • Add spaces between letters for readability
  • Right-align each row using leading spaces

Avoid

  • Using <br> for line breaks in CLI code (use PHP_EOL)
  • Allowing $rows > 26 without handling the alphabet size

Key Takeaways

1

The inner loop always starts from the top letter (E for 5 rows).

2

The outer loop increases the stop index, so each line becomes shorter.

3

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

❓ Frequently Asked Questions

Yes. Choose $rows so that $end = $rows - 1 points to the letter you want (e.g., 3 gives D as the first letter).
Because there are only 26 uppercase letters in range('A','Z'). Clamping avoids out-of-range indices.

Continue to PHP Alphabet Pattern 9

Next up: another pattern variation to practice loop boundaries.

Program 9 →
Did you know?

If you fix the start ($j = $end) and only change the stop ($j >= $i), you get triangles where the left edge stays constant.

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