A reverse fixed-start alphabet triangle always begins each row at the same top letter and counts down, while the stopping letter rises so the reverse tail gets shorter.
Remember
Rule: for stop letter i from A to last,
print last down through i
EDCBA
EDCB
EDC
ED
E ← 5 rows (left edge fixed at E)
Same widths as Program 7 (5, 4, 3, 2, 1), but Program 7 moves the left edge (EDCBA, DCBA, …). Here the left edge stays put. Compare also with Program 5, which shrinks forward prefixes from A.
Approach
How to Solve It
Two ways to emit the same shape — start with nested letter loops, then optionally reverse a prefix once and take shorter leading slices.
Method
Idea
Best for
Nested letter loops
Outer = rising stop; inner = $top..$stop downward
Learning, interviews, exams
strrev + substr(0, $len)
Build EDCBA… once, take shorter prefixes
Shorter demos once loops click
Pseudocode
Pseudocode
top = lastLetter
for stop from 'A' to top:
for ch from top down to stop:
print ch (no newline)
print newline
Cheat sheet
Goal
Pattern
Raise stop letter
for ($i = 'A'; $i <= $top; $i++)
Print $top..$i reverse
for ($j = $top; $j >= $i; $j--) echo $j;
End the row
echo PHP_EOL;
Top letter from rows
$top = chr(ord('A') + $rows - 1);
One-line row shortcut
strrev the A…top prefix, then substr(0, $len) while $len shrinks
1. Take and reverse the prefix.ABCDE reversed becomes EDCBA — the first printed row.
2. Shrink the leading slice.substr($top, 0, 5) is the full row; substr($top, 0, 4) drops the trailing A; and so on down to E.
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 = $i
Program 7 by mistake
If the inner loop starts at $i instead of $top, you print EDCBA, DCBA, … Use for ($j = $top; $j >= $i; $j--).
$j >= 'A'
No shrinking
Stopping at 'A' every time reprints the full reverse run. The stop must be the rising outer variable $i.
PHP_EOL early
Column of letters
If echo PHP_EOL is inside the inner loop, each letter lands on its own line. Use echo $j for letters; end 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 — top and stop coincide. A good sanity check.
Bad input
Use is_numeric
Bare (int) trim(fgets(STDIN)) turns letters into 0. Prefer is_numeric and clamp to 1…26.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(n²)
O(1)
strrev + substr (Example 3)
O(n²)
O(n) for the reversed prefix and temporary row strings
Total letters printed = n + (n−1) + … + 1 = n(n+1)/2, which is still quadratic in n.
Remember
Key Takeaways
Rule: always start at the top letter; raise the stop $i so each reverse row shortens.
vs Program 7: same widths — here the left edge stays fixed; there the left edge moves.
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 stop $i from 'A' to the top letter, echo top down through $i, then echo PHP_EOL.
Frequently Asked Questions
Because the inner loop always starts at the top letter (E in the 5-row example) and counts down. So the first printed character each row is always E.
The outer loop increases the stopping point for the inner loop. That shortens the tail each row, producing EDCBA, then EDCB, then EDC, and so on.
Program 7 changes the first letter each row (E, then D, then C…). Program 8 keeps the first letter fixed and only shortens the reverse tail.
Program 5 prints forward prefixes from A (ABCDE, ABCD, …). This pattern prints reverse prefixes from a fixed top (EDCBA, EDCB, …). Same shrinking widths; opposite letter direction and left edge.
Every row would print the full reverse run (EDCBA each time) with no shrinking.
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.
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?
Every row begins with the same top letter because the inner loop always starts there. The outer loop only raises the stopping point, so the tail shortens: EDCBA, EDCB, EDC, ED, E. Same widths as Program 7, but the left edge stays fixed.