PHP Reverse Alphabet Pyramid Pattern (Slice from E)
Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned reverse alphabet pyramid prints a growing reverse slice each row — from a single top letter down to a full E D C B A base — with padding so the block stays right-aligned.
Remember
Rule: for i from top down to 'A',
print pads for A..(i-1), then letters top..i (width 2)
E
E D
E D C
E D C B
E D C B A ← top = 'E' (monospace)
Companion to Program 22: same fixed-width grid (two spaces for pads + width-2 letter cells), but each row prints a reverse slice top..i instead of a running sequential counter.
Approach
How to Solve It
Two ways to emit the same shape — start with char loops, then optionally rewrite with int row indexes.
Method
Idea
Best for
Char loops
Outer $i; pad A..(i-1); letters top..i
Learning, interviews, exams
Int row index
Pad $n - $row; print $row letters from $top down
When you think in row numbers
Pseudocode
Pseudocode
for $i from $top down to 'A':
for $j from 'A' to ($i - 1):
echo " "
for $j from $top down to $i:
echo $j in width-2 field
echo PHP_EOL
1. Derive height.$n = ord($top) - ord('A') + 1 is 5 when $top is 'E'.
2. Pad by row number. Row 1 pads $n - 1 cells; the last row pads 0.
3. Letters from the top. Letter $k is chr(ord($top) - $k) — row 1 prints E; row 5 prints E..A.
Edge Cases & Pitfalls
Check these before calling the solution done.
Single space
Broken alignment
Pads must be " " (two spaces) to match width-2 letter cells. One space skews the pyramid in monospace output.
PHP_EOL inside
Broken outline
If echo PHP_EOL is inside either inner loop, each cell lands on its own line. Use echo for pads and letters; end the row only after both loops.
$j <= $i
Extra pad cell
The pad loop must stop at $j < $i, not $j <= $i, or you insert one too many empty cells.
$top = A
Single letter
Output is just a width-2 A cell. Pad loop never runs. A good sanity check.
$top < A
Empty output
Outer loop never runs if $top < 'A'. Validate A–Z before looping.
Bad input
Validate one letter
Empty, multi-character, or lowercase-only handling — trim, uppercase, and require length 1 in A–Z.
Analysis
Time and Space Complexity
Program
Time
Extra space
Char loops (Examples 1–2)
O(n²)
O(1)
Int row index (Example 3)
O(n²)
O(1)
For $n = ord($top) - ord('A') + 1 rows, each row does up to O(n) pad + letter writes. Letter total = 1 + 2 + … + n = n(n + 1)/2.
Remember
Key Takeaways
Rule: for $i from $top down to 'A', pad A..(i-1), then print top..i.
Fixed width: use " " pads and " " . $letter so every cell is two columns.
Break the row: call echo PHP_EOL only after both inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each row letter $i from top down to A, echo pads for A..(i-1), then letters top..i with width 2, then echo PHP_EOL.
Frequently Asked Questions
The first prints padding so the visible letters are right-aligned. The second prints letters from E (or your top letter) down to the current row letter i.
Each letter is printed as a leading space plus the character. Two spaces keep empty cells the same width so the pyramid aligns in monospace output.
Program 22 uses one running counter k++ across all rows (A..O). Program 23 prints a reverse slice each row using the loop variable j (E..i).
echo stays on the same line for pads and letters. echo PHP_EOL ends the row after both inner loops finish.
Yes. Change the second loop to always begin from your chosen top letter and decrement to the row letter — Example 2 does this with user input.
O(n²) for n rows because each row does O(n) work across the padding + letter loops.
Use trim(fgets(STDIN)), take the first character after strtoupper, require A–Z, and reject empty or multi-character tokens.
For top = E, when i is E you print 4 pad cells (A..D); when i is A you print 0. Pads = (ord($i) − ord('A')).
🤔
Did you know?
Outer i runs from E down to A. The first inner loop prints one " " per j with A <= j < i (so the block shifts left each row). The second loop prints letters from E down to i as width-2 cells (" " . $j).