Reverse Alphabet Decreasing Triangle in PHP

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops

What You’ll Learn

Print a triangle where the first row is the full reverse run from 'E' down to 'A', and each next row becomes shorter: EDCBA, DCBA, CBA, BA, A. This is the reverse-direction companion to Program 6 (ABCDE, BCDE, … ending at E). Compare also with Program 8. Includes a live preview, worked PHP examples, edge cases, and complexity.

Shape Rule

Rows shrink

First letter E → A; each row drops the left edge.

Outer Loop

i-- from top

Start letter lowers; row width shrinks.

Inner Loop

i..A

Print backward from the start down to A.

vs Program 6

Mirror

Same widths; letters run reverse to A.

Live Preview

Rows 1–10

Pick a row count and draw EDCBA…A live.

O(n²)

Complexity

n+(n-1)+…+1 printed characters total.

Introduction

A reverse alphabet decreasing triangle keeps a fixed right edge at A while the left edge walks backward — each row is a shorter reverse run toward A.

In PHP you lower the start letter with the outer loop (i--) and print from i down to 'A' with the inner loop.

Why it matters?

It pairs with Program 6 to show that flipping only letter direction turns forward suffixes into reverse runs with the same shrinking geometry.

Key Highlights

Outer

i from top down to A.

Inner

j from i down to A.

Edge

Every row ends at A.

Output

EDCBA … A

In short: for each start letter $i from $top down to 'A', print $j from $i down to 'A', then call echo PHP_EOL.

📝 Problem & Approach

Given a row count (or fixed top E), print a shrinking triangle where each row is a reverse run ending at A.

PHP
// Five rows (top = E)
// EDCBA
// DCBA
// CBA
// BA
// A

Inputs & Outputs

ItemTypeDescription
rows / $topint / charNumber of rows; top letter is 'A' + rows - 1 (E for 5).
Printed outputtextShrinking reverse runs from top..A down to A alone.

Minimal workflow

Pseudocode
$top = chr(ord('A') + $rows - 1)
for i from top down to 'A':     // start letter lowers
    for j from i down to 'A':   // reverse run each row
        print j
    print newline

Approach comparison

ApproachIdeaBest for
Char nested loopsOuter i--, inner j-- from i to AMatching this classic sample
Mirror of Program 6Same bounds as Program 6 but count down instead of upWhen teaching direction flips

⚡ Quick Reference

GoalPattern
Fixed A–Efor ($i = 'E'; $i >= 'A'; $i--)
Reverse to Afor ($j = $i; $j >= 'A'; $j--) echo $j;
User rows$top = chr(ord('A') + $rows - 1); then loop $i from $top down
Forward insteadSee Program 6 (j from i up to end)
Fixed start at ESee Program 8

📋 Prog 4 vs Prog 6 vs Prog 7

Three reverse-friendly triangles with different growth and edges.

Program 4
grow i..A

A, BA, CBA — growing reverse

Program 6
shrink i..end

ABCDE, BCDE — forward suffixes

Program 7
shrink i..A

EDCBA, DCBA — reverse to A

echo PHP_EOL
break

Ends the row after i..A finishes

Context

When This Pattern Shows Up

Reach for this when teaching a lowering start bound with a descending letter run to A.

  1. After Program 6

    Keep the shrinking start idea; flip letter direction to reverse.

  2. Fixed A-edge drills

    Practice reverse runs that always end at the same letter.

  3. Bridge to Program 8

    Next keeps E fixed on the left: EDCBA, EDCB, EDC, …

  4. Double-decrement practice

    Mix i-- with j-- in the same program.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one lowering start plus a descending inner loop is the cleanest way to shrink a reverse alphabet triangle with a fixed A on the right.

🔮 Live Preview

Choose 1–10 rows and draw the reverse alphabet decreasing triangle in the browser.

Try 5 (classic EDCBA…A) or 4 (DCBA…A). Max 10 keeps the preview readable.

Live result
Press "Draw pattern".

Examples Gallery

Three complete PHP programs — fixed A–E, user-chosen row count, and a spaced-letter variant. Click View Output to reveal sample console results.

📚 Getting Started

Print five shrinking reverse rows that always end at A.

Example 1 — Fixed Top E

Outer loop chooses the starting letter (E down to A). Inner loop prints from that start down to A.

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

How It Works

When i = 'C', the inner loop prints C, B, ACBA. When i = 'A', it prints only A.

📈 Practical Variant

Let the user choose how many rows to print.

Example 2 — Row Count Input

Read the number of rows and compute $top = chr(ord('A') + $rows - 1). Prefer checking is_numeric() before casting trim(fgets(STDIN)) in real apps.

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

For 4 rows, $top becomes 'D'. Cap $rows at 26 so $top stays within A–Z.

⚡ Readability Variant

Same triangle with spaces between letters.

Example 3 — Spaced Letters

Print a trailing space after each letter so columns are easier to scan.

PHP
<?php
$top = 'E';

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

How It Works

Loop bounds are unchanged — only the printed unit becomes $j . " ". Trim trailing spaces later if you need a compact line.

🧠 How the Algorithm Prints Rows

1

Outer loop sets the row start

i runs from top down to 'A'. That is the first letter on each row.

Start moves left
2

Inner loop prints down to A

j runs from i down to 'A', so each row prints reverse alphabetical order.

Descending letters
3

New line

echo PHP_EOL ends each row.

Line break
4

Right edge stays A

Because the inner loop always stops at 'A', every row ends on A while the left side walks backward.

Alignment
=

Shrinking reverse rows

Total printed characters are n+(n-1)+…+1, so time complexity is O(n²).

🔎 Worked Walkthrough — Top = E (5 rows)

Trace each start letter and the resulting reverse run down to A.

i (start)Inner rangePrinted row
EE..AEDCBA
DD..ADCBA
CC..ACBA
BB..ABA
AA..AA

Row lengths are 5, 4, 3, 2, 1. The right edge is always A.

Use Cases

Where this reverse decreasing alphabet triangle shows up beyond the homework prompt.

1. Direction Labs

Clearest demo of flipping Program 6 to reverse.

Example: change j-- to j++ and compare with Program 6.

2. Edge Practice

Fixed right edge at A with a moving left edge.

Example: stack next to Program 6’s fixed E edge.

3. Char Bounds

Practice inclusive descending ranges ending at 'A'.

Example: off-by-one if you stop at 'B'.

4. Input Scaling

Map row count to top letter with 'A' + rows - 1.

Example: scale from 5 to 8 without rewriting loops.

5. Complexity Intuition

Triangle sums make O(n²) easy to see.

Example: 15 letters for 5 rows.

6. Series Continuity

Sits between Programs 6 and 8 in the alphabet set.

Example: revisit Program 4.

Pro Tip: say “lower the start, then walk down to A” before coding — that story prevents writing a forward inner loop by habit.

Advantages

Why this pattern earns a spot early in the alphabet-pattern series.

  1. 1. Instant Visual Feedback

    A forward row or missing A shows up immediately.

  2. 2. Clear Pair with Program 6

    Same shrinking start; only letter direction differs.

  3. 3. Scales Cleanly

    Change the top letter or row count and the whole triangle shrinks from the left.

  4. 4. Beginner-Friendly

    No padding or diagonal checks — just two char loops.

Pro Tip: master Program 6 first; this page is mostly “same outer idea, count the letters down instead of up.”

Usage Tips

Small habits that keep reverse decreasing alphabet triangles clean.

  1. 1. Count Down to A Inclusive

    Use j >= 'A' so every row still ends with A.

  2. 2. Start the Inner Loop at i

    Starting at a fixed top letter turns this into Program 8.

  3. 3. Set $top = chr(ord('A') + $rows - 1)

    Forgetting the - 1 makes the first row one letter too long.

  4. 4. Cap Rows at 26

    Keep the top letter inside A–Z when taking user input.

  5. 5. Prefer is_numeric()

    Validate row input instead of blind (int) trim(fgets(STDIN)).

Pro Tip: if you see ABCDE, BCDE, CDE, the inner loop is still incrementing — switch to j--.

Common Pitfalls

Mistakes that commonly break reverse decreasing alphabet triangles.

  1. 1. Using j++ in the Inner Loop

    Prints Program 6 instead of EDCBA / DCBA.

    → Use for ($j = $i; $j >= 'A'; $j--).

  2. 2. Starting the Inner Loop at Top

    Produces Program 8-style fixed-start rows (EDCBA, EDCB, …).

    → Always start at j = i.

  3. 3. Wrong $top Formula

    Using ord('A') + $rows without - 1 overshoots.

    → Use $top = chr(ord('A') + $rows - 1).

  4. 4. Blind Integer Cast

    Empty or non-numeric input becomes 0 without warning.

    → Check is_numeric() and validate range.

  5. 5. Forgetting echo PHP_EOL

    All letters dump onto one line.

    → Call echo PHP_EOL after each inner loop.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single letter

Output is just A.

rows = 5

Classic sample

EDCBA down to A (Example 1).

rows = 4

Smaller triangle

DCBA down to A (Example 2).

rows > 26

Past Z

Cap or reject — top leaves the alphabet.

Bad input

Empty / non-numeric

Validate with is_numeric().

Lowercase

edcba … a

Swap 'A' for 'a' in both loops.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Flip to Program 6

  • Change only the inner loop to j++
  • Confirm you get ABCDE, BCDE, CDE

2. Compare with Program 8

  • Start the inner loop at top instead of i
  • See Program 8

3. Scale to 8 rows

  • Use the input version
  • Check the first row is HGFEDCBA

4. Grow instead of shrink

  • Outer i++ from A with inner j--
  • That becomes Program 4

Notes

  • Start lowers. Outer i-- moves the left edge backward each row.
  • The inner loop always stops at 'A', so the right edge is vertical.
  • Row lengths are n, n-1, …, 1 — same as Program 6, reverse letters.
  • Next up: Alphabet Pattern 8 keeps E fixed on the left.

Quick Takeaway: lower the start letter with the outer loop, then walk down to A with the inner loop — that alone builds EDCBA, DCBA, …, A.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Inline / input (Examples 1–2)O(n²)O(1)
Spaced letters (Example 3)O(n²)O(1)

For n rows you print n+(n-1)+…+1 = n(n+1)/2 characters, so total work is O(n²).

Wrap Up

🎉 Conclusion

The reverse alphabet decreasing triangle keeps a fixed A on the right while the start letter walks backward: each row is a shorter reverse run. Master the classic EDCBA…A sample, then try user input and the spaced rewrite.

Practice the three examples above, then continue to Alphabet Pattern 8.

Outer i from top down to A, inner j from i down to A, then break each line.

💡 Best Practices

✅ Do

  • Start the outer loop at $top and count down to 'A'
  • Start the inner loop at i and count down to 'A'
  • Use $top = chr(ord('A') + $rows - 1) for input versions
  • Cap user row counts at 26
  • State O(n²) when asked about complexity

❌ Don’t

  • Use j++ when you want EDCBA / DCBA
  • Start the inner loop at top (that becomes Program 8)
  • Forget the - 1 in the top formula
  • Skip validating row-count input
  • Call echo PHP_EOL inside the letter loop

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the reverse alphabet decreasing triangle the beginner-friendly way.

5
Core concepts
= 02

Inner

j from i to A

Code
1 03

Edge

Every row ends at A

Shape
R 04

vs Prog 6

Same widths, reverse

Compare
O 05

Complexity

O(n²) time

Analysis

❓ 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, so the first line is EDCBA and each next line drops the previous leftmost letter.
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.
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…).
Because the inner loop condition includes j == 'A', so the final character printed on each row is always A.
O(n²) for n rows, because total printed characters are n(n+1)/2.
Yes. Read rows from the CLI and set $top = chr(ord('A') + $rows - 1). Then loop $i from $top down to 'A' and echo $j from $i down to 'A'.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int, require n ≥ 1, and cap at 26 so the top letter stays within A–Z.

Did you Know? 🔊

This triangle prints reverse letters and shrinks each row: EDCBA, DCBA, CBA, BA, A. Both loops run backward so letters move toward 'A' on every line.

Explore More PHP Alphabet Patterns!

Reverse loops are just as useful as forward loops for pattern printing.

All Alphabet Patterns →

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