A symmetric alphabet pyramid prints centered palindromic rows: each line climbs from A to a peak letter, then mirrors back down to A without repeating the peak.
Remember
Rule: spaces + A..i + (i−1)..A
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA ← top = 'E'
Three stages per row: leading spaces for centering, ascending A..i, then descending (i − 1)..A. Compare with Program 24 (similar palindromes) and Program 31 (hollow V).
Approach
How to Solve It
Two ways to emit the same pyramid — a classic bridge variable (--$n), or an explicit descending loop.
Method
Idea
Best for
Bridge variable
After A..i, set $n = $k - 1 and print $alpha[--$n]
Matching classic textbook / exam style
Explicit mirror
Loop $p from $i - 1 down to 0
Clearer demos once the skip-center rule clicks
Pseudocode
Pseudocode
$end = ord($top) - ord('A')
for $i from 0 to $end:
print ($end - $i) spaces
for $k from 0 to $i: echo $alpha[$k] // A..i
for $p from $i-1 to 0: echo $alpha[$p] // (i-1)..A
echo PHP_EOL
1. Same three stages. Spaces, then ascending, then mirror — only the mirror syntax changes.
2. Peak stays unique.$p starts at $i - 1, so the center letter is never printed twice.
3. Same output. Matches the bridge-variable version exactly.
Edge Cases & Pitfalls
Check these before calling the solution done.
Mirror from i
Double center letter
If the descending loop starts at $i instead of $i - 1, you get ABCCBA. Skip the peak.
Spaces off
Left-aligned pyramid
Keep for ($j = $end; $j > $i; $j--). Starting at $end - 1 or using >= shifts the centering.
PHP_EOL inside
Column of characters
If echo PHP_EOL is inside any of the three loops, each character lands on its own line. Use echo until the row is done.
top = A
Single A
When $end = 0, you print one row: A (no spaces, no mirror). A good sanity check.
Wrong n
Bridge off by one
After ascending, $k is already $i + 1. Use $n = $k - 1, then --$n — not $n = $k.
Bad input
Validate one letter
Trim, uppercase, and require length 1 in A–Z — reject empty or multi-character tokens.
Analysis
Time and Space Complexity
Program
Time
Extra space
Bridge variable (Examples 1–2)
O(n²)
O(1) (plus the fixed alphabet table)
Explicit mirror (Example 3)
O(n²)
O(1)
For $n = $end + 1 rows, each row prints O(n) spaces and letters — quadratic in n.
Remember
Key Takeaways
Rule: spaces + A..i + (i − 1)..A.
Skip the peak once: mirror starts at $i - 1 (or --$n after $n = $k - 1).
Break the row: call echo PHP_EOL only after all three stages.
Complexity:O(n²) time; O(1) extra space.
One line: pad, print A up to the peak, then mirror back to A without reprinting the peak.
Frequently Asked Questions
It prints A..i ascending, then prints (i-1)..A descending. This mirrors the left half without repeating the center letter.
After the ascending loop, $k is one past the peak letter. Setting $n = $k-1 makes $n equal the peak, and then --$n starts the descending half from the previous letter.
The padding shifts each row to the right so the pyramid is centered under the widest row.
O(n²) for n rows because each row prints O(n) spaces and letters.
After printing A..i, the mirror part starts from i-1 down to A. That avoids duplicating the peak letter.
echo stays on the same line. echo PHP_EOL ends the current line. Spaces and letters use echo; the row break uses echo PHP_EOL after all three stages.
Ascending prints i+1 letters and descending prints i letters, so the row has 2i+1 letters before counting spaces.
Program 18 is another palindromic alphabet pyramid approach. This page follows the classic bridge-variable style with $n = $k-1 and --$n.
🤔
Did you know?
Each row has three stages: leading spaces for centering, then letters A..i ascending, then letters (i-1)..A descending. The reference keeps a bridge variable $n = $k - 1 after the ascending loop and prints --$n to avoid duplicating the center letter.