Alphabet Pattern A to EDCBA in PHP

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Reverse along row

What You'll Learn

This PHP program prints a triangle where the first letter of each row moves forward (A, B, C, D, E), but letters in the row are printed backward down to A.

For rows = 5, the output is A, BA, CBA, DCBA, EDCBA.

⭐ Pattern Output

When you run the program with rows = 5:

Output
A
BA
CBA
DCBA
EDCBA
1

Complete PHP Program

Fixed rows = 5 version:

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

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

🧠 How It Works

1

Alphabet array

$alpha = range('A', 'Z'); gives you an indexable list of letters.

Setup
2

Outer loop picks the row start

Row $i starts from $alpha[$i] (A, then B, then C...).

Rows
3

Inner loop prints backward to A

for ($j = $i; $j >= 0; $j--) prints indices $i, $i-1, ..., 0, so each row ends at A.

Reverse
=

Reverse-order rows

Total printed letters are 1+2+…+n = n(n+1)/2, so runtime is O(n²).

2

Variation — User Input (CLI) Version

Read rows from standard input (works when you run the file from terminal using php):

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

💡 Tips for Enhancement

Try These

  • Compare with Program 1 (forward letters within the row)
  • Switch to lowercase using range('a', 'z')
  • Add spaces between letters for readability
  • Right-align the triangle with leading spaces
  • Print the same logic using chr() instead of range()

Avoid

  • Allowing $rows > 26 without handling wrap-around
  • Using <br> in CLI examples (use PHP_EOL)
  • Off-by-one errors ($j >= 0 matters)
  • Mixing HTML output with CLI output without intent

Key Takeaways

1

Row i starts at 'A' + i and prints down to 'A'.

2

The inner loop counts backward, so letters are reversed within each row.

3

Every row ends at A, so the right edge is fixed.

4

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

❓ Frequently Asked Questions

Program 3 keeps the right edge fixed at E and prints forward along the row (E, DE, CDE...). This program keeps the right edge fixed at A and prints backward along the row (A, BA, CBA...).
If you want to stay within A..Z, keep rows at 26 or less. For larger counts you’ll need a different scheme (e.g., repeating letters or using AA, AB...).
It’s O(n²) for n rows, because the program prints 1+2+…+n letters in total.

Next: PHP Alphabet Pattern 5

Continue to Program 5 for the next alphabet pattern in PHP.

Program 5 →
Did you know?

This is the mirror idea of patterns where every row ends at the same letter: here, every row ends at A.

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