PHP Reverse Alphabet Triangle Pattern (Decreasing)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A reverse alphabet decreasing triangle prints each row backward down to A, while the starting letter moves one step earlier each row — so the left edge slides inward.

Remember
Rule: for start letter $i from last down to A,
      print $i down through A

EDCBA
DCBA
CBA
BA
A         ← 5 rows (every row ends at A)

Same widths as Program 6 (5, 4, 3, 2, 1), but Program 6 prints forward to a fixed end (ABCDE, BCDE, …). Here both loops decrement. Compare with Program 8, which keeps E fixed on the left and shortens the tail.

How to Solve It

Two ways to emit the same shape — start with nested letter loops, then optionally reverse a prefix once and slice.

MethodIdeaBest for
Nested letter loopsOuter = lowering start; inner = start..A downwardLearning, interviews, exams
strrev + substr($top, $i)Build EDCBA… once, drop one left letter each rowShorter demos once loops click

Pseudocode

Pseudocode
for $start from $lastLetter down to 'A':
    for $ch from $start down to 'A':
        echo $ch
    echo PHP_EOL

Cheat sheet

GoalPattern
Lower start letterfor ($i = $top; $i >= 'A'; $i--)
Print $i..A reversefor ($j = $i; $j >= 'A'; $j--) echo $j;
End the rowecho PHP_EOL;
Top letter from rows$top = chr(ord('A') + $rows - 1);
One-line row shortcutstrrev the A…top prefix, then substr($top, $i)
Forward twinProgram 6 — print $i..end upward

Printing Letters vs Starting a New Line

APIEffectUse for
echoStays on the same lineEach letter
echo PHP_EOLEnds the current lineAfter the inner loop

Print letters without a newline, then end the row once.

Live Preview

Change the row count and the reverse decreasing triangle updates instantly — capped at 26 letters (A–Z).

Whole numbers from 1 to 26. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 letters
EDCBA
DCBA
CBA
BA
A

Worked Walkthrough — $rows = 4

Trace each outer-loop start letter as $i counts down from 'D' to 'A'.

Start $iInner $jPrinted rowLetters
'D'D..ADCBA4
'C'C..ACBA3
'B'B..ABA2
'A'A..AA1

Total letter prints: 4 + 3 + 2 + 1 = 10 = 4×5/2 — same triangular count as Programs 1 and 6.

PHP Programs

Three complete programs: fixed top letter, CLI input, and a strrev + substr shortcut. Use View Output to reveal sample results.

Example 1 — Fixed from 'E' down to 'A'

Hard-coded top letter — both loops decrement; every row ends at A.

PHP
<?php
for ($i = 'E'; $i >= 'A'; $i--) {
    for ($j = $i; $j >= 'A'; $j--) {
        echo $j;
    }
    echo PHP_EOL;
}

How It Works

1. Outer loop picks the start letter. $i runs from 'E' down to 'A' — longest row first.

2. Inner loop walks down to A. For each start, $j runs from $i down to 'A', so the row is $i..A in reverse.

3. Print letters, then break the line. echo $j stays on the row; echo PHP_EOL after the inner loop starts the next (shorter) row.

When $i = 'E' you get EDCBA; when $i = 'A' you get A.

Example 2 — User Input Version

Read the row count at runtime. Prefer validating with is_numeric and clamp to 26 (shown in the tip below).

PHP
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));
$top = chr(ord('A') + $rows - 1);

for ($i = $top; $i >= 'A'; $i--) {
    for ($j = $i; $j >= 'A'; $j--) {
        echo $j;
    }
    echo PHP_EOL;
}

How It Works

1. Prompt and read. Ask for a row count, then cast the trimmed line to an int.

2. Map rows to a top letter. $top = chr(ord('A') + $rows - 1) — for $rows = 4, $top is 'D'.

3. Same reverse-shrink core. Only the source of $top changes — the print logic matches Example 1.

4. Safer input tip. Prefer:

Safer input
$raw = trim(fgets(STDIN));
if (!is_numeric($raw)) {
    echo "Enter a whole number from 1 to 26." . PHP_EOL;
    exit;
}
$rows = (int) $raw;
if ($rows < 1 || $rows > 26) {
    echo "Enter a whole number from 1 to 26." . PHP_EOL;
    exit;
}

Example 3 — strrev + substr($top, $i)

Build the first reverse row once, then drop one left letter each time — same shape, no nested letter loop.

PHP
<?php
$rows = 5;
$letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$top = strrev(substr($letters, 0, $rows));

for ($i = 0; $i < $rows; $i++) {
    echo substr($top, $i) . PHP_EOL;
}

How It Works

1. Take and reverse the prefix. ABCDE reversed with strrev becomes EDCBA — the first printed row.

2. Slice from index $i. substr($top, 0) is the full row; substr($top, 1) drops E; and so on down to A.

3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

$j++

Program 6 by mistake

If the inner loop increments instead of decrements, you print forward suffixes. Use for ($j = $i; $j >= 'A'; $j--).

Fixed start

Program 8 shape

If the outer loop stays on 'E' and only shortens the stop, you get EDCBA, EDCB, … Advance (lower) the start with $i--.

PHP_EOL inside

Column of letters

If echo PHP_EOL is inside the inner loop, each letter lands on its own line. Use echo for letters; break the row only after the inner loop.

$rows > 26

Past Z

chr(ord('A') + $rows - 1) leaves A–Z when $rows > 26. Clamp or reject in interactive programs.

$rows = 1

Single A

Output is just A — start and end coincide. A good sanity check.

Bad input

Use is_numeric

Bare (int) fgets(STDIN) turns letters into 0 — validate before the outer loop.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(1)
strrev + substr (Example 3)O(rows²)O(rows) for the reversed prefix and temporary row strings

Total letters = n + (n - 1) + … + 1 = n(n + 1)/2 — quadratic in n. Same totals as Programs 1 and 6.

Key Takeaways

  • Rule: lower start letter $i; print $i down through A each row.
  • vs Program 6: same widths — here letters run backward to A; there they run forward to a fixed end.
  • Break the row: call echo PHP_EOL only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space for nested loops.

One line: for $i from the top letter down to 'A', print $i down through A, then echo PHP_EOL.

Frequently Asked Questions

The outer loop picks the starting letter (E, then D, then C…). The inner loop prints from that start down to A. Each row drops the previous leftmost letter and becomes shorter.
The outer loop decreases the starting letter each row, and the inner loop decreases letters within that row down to A. That matches the reverse-alphabet output.
Because the inner loop condition includes $j == 'A', so the final character printed on each row is always A.
Program 6 prints forward suffixes ending at E (ABCDE, BCDE, …). This pattern prints reverse runs ending at A (EDCBA, DCBA, …). Same shrinking widths; opposite letter direction.
Program 8 always starts each row at E and shortens the tail (EDCBA, EDCB, EDC). Program 7 changes the first letter each row (E, then D, then C…).
echo stays on the same line. echo PHP_EOL ends the current line. Letters use echo; the row break uses echo PHP_EOL after the inner loop.
O(n²) for n rows, because total printed characters are n(n+1)/2.
Yes. Reverse the A…top prefix with strrev, then echo substr($top, $i) . PHP_EOL while $i advances. Nested letter loops are better for learning; strrev is a handy shortcut later.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int, then clamp rows between 1 and 26 so bad input does not walk past Z.

Did you know?

Both loops run backward: the outer loop lowers the start letter; the inner loop prints down to A. For 5 rows: EDCBA, DCBA, CBA, BA, A. Same widths as Program 6, opposite letter direction.

Next: Reverse Fixed Start

Keep E on the left and shorten the reverse tail each row.

Program 8 tutorial →

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