PHP Palindromic Alphabet Pyramid Pattern (Center A)

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

What Is This Pattern?

A palindromic alphabet pyramid prints mirrored letter rows that grow around a single center A: each line reads the same forward and backward.

Remember
Rule: for peak i from 'A' to top,
      print i..B (descending), then A..i (ascending)

A
BAB
CBABC
DCBABCD
EDCBABCDE     ← 5 rows (top = 'E')

The descending wing stops before A so the center letter is not printed twice. Compare with Program 18 (different wing order) and add pads (or see Program 16) if you want the shape centered.

How to Solve It

Two ways to emit the same palindrome — classic char loops, then optionally space each letter for a wider look.

MethodIdeaBest for
Descend + ascendLeft wing i..B; right wing A..iLearning, interviews, exams
Spaced lettersSame wings; print $ch . " "Clearer demos in dense terminals

Pseudocode

Pseudocode
for $i from 'A' to $top:
    for $j from $i down to 'B':
        echo $j
    for $j from 'A' to $i:
        echo $j
    echo PHP_EOL

Cheat sheet

GoalPattern
Pick the peakfor ($i = 'A'; $i <= 'E'; $i++)
Left wingfor ($j = $i; $j > 'A'; $j--) echo $j;
Center + rightfor ($j = 'A'; $j <= $i; $j++) echo $j;
End the rowecho PHP_EOL;
Row length2 * (ord($i) - ord('A')) + 1 (odd widths)
Peak from rows$peak = chr(ord('A') + $r);
Avoid double AUse $j > 'A', not $j >= 'A'

Printing Letters vs Starting a New Line

APIEffectUse for
echoStays on the same lineEach letter on both wings
echo PHP_EOLEnds the current lineAfter both inner loops

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

Live Preview

Change the height and the palindrome updates instantly — including the letter total (n²).

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

Live result 5 rows · 25 letters
A
BAB
CBABC
DCBABCD
EDCBABCDE

Worked Walkthrough — A–E

Trace each row’s left wing, center + right wing, and full line.

$iLeft (i..B)Right (A..i)Printed row
A(empty)AA
BBABBAB
CCBABCCBABC
DDCBABCDDCBABCD
EEDCBABCDEEDCBABCDE

Lengths: 1 + 3 + 5 + 7 + 9 = 25 = 5². That odd-number sum is why time is O(n²).

PHP Programs

Three complete programs: fixed A–E, CLI row-count input, and a spaced-letter variant. Use View Output to reveal sample results.

Example 1 — Fixed A–E

Two loops per row: descending (i..B), then ascending (A..i). The descending loop stops before A.

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

    for ($j = 'A'; $j <= $i; $j++)
        echo $j;

    echo PHP_EOL;
}
?>

How It Works

1. Outer loop picks the peak. $i runs from 'A' to 'E' — one row per peak letter.

2. Left wing descends. Print $j from $i down while $j > 'A' (stops at B).

3. Center + right ascend. Print $j from 'A' through $i — that supplies the single center A.

4. Break the line. echo PHP_EOL; after both wings starts the next row.

When $i = 'C', the left wing prints CB and the right wing prints ABC → CBABC.

Example 2 — Row Count Input

Cap at 26 for A–Z. Prefer is_numeric in real apps.

PHP
<?php
echo "Enter number of rows (1..26): ";
$n = (int) trim(fgets(STDIN));
if ($n < 1) exit;
if ($n > 26) $n = 26;

for ($r = 0; $r < $n; $r++) {
    $peak = chr(ord('A') + $r);

    for ($ch = $peak; $ch > 'A'; $ch--)
        echo $ch;

    for ($ch = 'A'; $ch <= $peak; $ch++)
        echo $ch;

    echo PHP_EOL;
}
?>

How It Works

1. Prompt and clamp. Read a row count and keep it in 1–26 so peaks stay in A–Z.

2. Map index to peak. Row $r uses $peak = chr(ord('A') + $r).

3. Same wing core. Descend/ascend rules match Example 1 — only the number of peaks changes.

4. Safer input tip. Prefer:

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

Example 3 — Spaced Letters

Same palindrome with a space after each letter for a wider look.

PHP
<?php
for ($i = 'A'; $i <= 'E'; $i++) {
    for ($j = $i; $j > 'A'; $j--)
        echo $j . " ";

    for ($j = 'A'; $j <= $i; $j++)
        echo $j . " ";

    echo PHP_EOL;
}
?>

How It Works

1. Same wings. Still descend i..B, then ascend A..i.

2. Format only. $j . " " adds a trailing space after each letter.

3. Optional polish. Trim the final space per row if you need a clean line end.

Edge Cases & Pitfalls

Check these before calling the solution done.

$j >= 'A'

Duplicate center A

If the descending loop uses $j >= 'A', you get BAAB or CBABBC. Keep $j > 'A'.

Wing order

Wrong palindrome style

Ascending first then descending gives Program 18-style rows (e.g. ABCBA) instead of CBABC.

PHP_EOL inside

Column of letters

If echo PHP_EOL is inside either wing loop, each letter lands on its own line. Use echo for letters; end the row only after both loops.

$n = 1

Single A

Left wing never runs; output is just A. A good sanity check.

$n > 26

Past Z

Clamp or reject so peaks stay in A–Z.

Bad input

Use is_numeric

Casting non-numeric input to int becomes 0. Prefer is_numeric and require 1–26.

Time and Space Complexity

ProgramTimeExtra space
Descend + ascend (Examples 1–2)O(n²)O(1)
Spaced letters (Example 3)O(n²)O(1)

Total letters = 1 + 3 + … + (2n - 1) = n². Spaced output prints the same letters plus spaces — still quadratic in $n.

Key Takeaways

  • Rule: for peak $i, print i..B then A..i.
  • Single center A: stop the descending loop at $j > 'A'.
  • Break the row: call echo PHP_EOL only after both wings.
  • Complexity: O(n²) time from n² letters; O(1) extra space.

One line: for each peak $i, echo descending i..B, then ascending A..i, then echo PHP_EOL.

Frequently Asked Questions

The first loop prints descending letters from the row peak down to B. The second prints ascending from A through i. Together they mirror around a single A.
The forward loop already prints A. Stopping the descending loop before A avoids duplicating the center character.
The descending loop stops at j > 'A', and the ascending loop starts at 'A'. That prevents printing A twice at the join.
For row letter i, length is 2*(ord($i)-ord('A'))+1, so rows grow as 1, 3, 5, 7, 9 for A..E.
Change the outer loop upper bound from 'E' to your target letter (like 'H'), or use the row-count input variant.
Program 18 typically builds A..peak then peak-1..A. This pattern descends to B first, then ascends A..i, still mirroring around a single A.
O(n²) for n rows because total printed characters are 1+3+...+(2n-1) = n².
No — the classic version is left-aligned with no leading spaces. Add pad spaces (or see Program 16) if you want a centered pyramid.

Did you know?

Outer i runs A.. E. First inner loop prints the left wing (i down to B) by stopping at j > 'A'. Second inner loop prints A.. i. Because the reverse loop stops before A, the center A is not duplicated.

Next: Sequential Decreasing

Move from mirrored wings to a running $k++ stream with shrinking row lengths.

Program 25 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