PHP Reverse Alphabet Pyramid Pattern (Centered)

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

What Is This Pattern?

A reverse centered alphabet pyramid prints Program 28’s layered rows down to the A-center line, then mirrors them back up to the top letter — without printing the center twice.

Remember
Rule: PrintRow(i) = left k..0 + right 1..k with (j > i ? j : i)
      Phase 1: i = k..0   Phase 2: i = 1..k

E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E   ← center once
E D C B B B C D E
E D C C C C C D E
E D D D D D D D E
E E E E E E E E E     ← top = 'E' (9×9)

Program 28 is exactly the upper half. The lower phase starts at $i = 1 (B) so the A-center row is not duplicated.

How to Solve It

Two ways to emit the same diamond — duplicate the row body in both phases, or factor helpers.

MethodIdeaBest for
Two phases inlineUpper $k..0, lower 1..$k, same row bodyLearning, interviews, exams
print_cell + print_rowOne place owns the floor rule and both halvesCleaner demos once the rule clicks

Pseudocode

Pseudocode
$k = ord($top) - ord('A')
define PrintRow($i):
    for $j from $k down to 0: echo ($j > $i ? $alpha[$j] : $alpha[$i])
    for $j from 1 to $k:      echo ($j > $i ? $alpha[$j] : $alpha[$i])
    echo PHP_EOL

for $i from $k down to 0: PrintRow($i)   // upper
for $i from 1 to $k:      PrintRow($i)   // lower

Cheat sheet

GoalPattern
Top index$k = ord($top) - ord('A'); (4 for E)
Floor rule$j > $i ? $alpha[$j] : $alpha[$i]
Upper phasefor ($i = $k; $i >= 0; $i--) print_row(...);
Lower phasefor ($i = 1; $i <= $k; $i++) print_row(...);
Skip center twiceLower starts at 1, not 0
SizeRows = width = 2 * $k + 1
Upper half onlyProgram 28

Printing Letters vs Starting a New Line

APIEffectUse for
echoStays on the same lineEach letter (and its trailing space)
echo PHP_EOLEnds the current lineAfter both half-loops of a row

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

Live Preview

Change the top letter and the reverse-centered diamond updates instantly — rows = width = 2k + 1.

One letter from A to F. Tap a chip or type a letter — the preview redraws as you go.

Live result Top E · 9 rows · width 9
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
E D C B B B C D E
E D C C C C C D E
E D D D D D D D E
E E E E E E E E E

Worked Walkthrough — Top = E ($k = 4)

Trace each floor and the resulting 9-letter line across both phases.

Phase$iFloorPrinted row
Upper4EE E E E E E E E E
Upper3DE D D D D D D D E
Upper2CE D C C C C C D E
Upper1BE D C B B B C D E
Upper0AE D C B A B C D E
Lower1BE D C B B B C D E
Lower2CE D C C C C C D E
Lower3DE D D D D D D D E
Lower4EE E E E E E E E E

Width is always 2×4 + 1 = 9. Total rows are 2×5 − 1 = 9. The A-center row appears only once (upper phase).

PHP Programs

Three complete programs: fixed A–E, top-letter input, and a helper-function rewrite. Use View Output to reveal sample results.

Example 1 — Fixed A–E

Same row logic as Program 28, printed in two phases to complete the reverse centered pyramid.

PHP
<?php
$k = 4; // index for 'E'
$alpha = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

// Upper half (E down to A)
for ($i = $k; $i >= 0; $i--) {
    for ($j = $k; $j >= 0; $j--)
        echo ($j > $i ? $alpha[$j] : $alpha[$i]) . " ";
    for ($j = 1; $j <= $k; $j++)
        echo ($j > $i ? $alpha[$j] : $alpha[$i]) . " ";
    echo PHP_EOL;
}

// Lower half (B up to E) — skip repeating the A row
for ($i = 1; $i <= $k; $i++) {
    for ($j = $k; $j >= 0; $j--)
        echo ($j > $i ? $alpha[$j] : $alpha[$i]) . " ";
    for ($j = 1; $j <= $k; $j++)
        echo ($j > $i ? $alpha[$j] : $alpha[$i]) . " ";
    echo PHP_EOL;
}

How It Works

1. Fix the top index. $k = 4 means the highest letter is E.

2. Upper phase. Floor $i runs from $k down to 0 — same as Program 28 through the A-center row.

3. Lower phase. Floor $i runs from 1 to $k so the center line is not printed twice.

4. Same row body. Both phases use left $k..0 and right 1..$k with $j > $i ? $alpha[$j] : $alpha[$i].

Example 2 — Top Letter Input

Works for A..top with the same two-phase pyramid. Prefer validating a single A–Z character in real apps.

PHP
<?php
echo "Enter top letter (like E): ";
$top = strtoupper(trim(fgets(STDIN)))[0];

$k = ord($top) - ord('A');
$alpha = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

for ($i = $k; $i >= 0; $i--) {
    for ($j = $k; $j >= 0; $j--)
        echo ($j > $i ? $alpha[$j] : $alpha[$i]) . " ";
    for ($j = 1; $j <= $k; $j++)
        echo ($j > $i ? $alpha[$j] : $alpha[$i]) . " ";
    echo PHP_EOL;
}

for ($i = 1; $i <= $k; $i++) {
    for ($j = $k; $j >= 0; $j--)
        echo ($j > $i ? $alpha[$j] : $alpha[$i]) . " ";
    for ($j = 1; $j <= $k; $j++)
        echo ($j > $i ? $alpha[$j] : $alpha[$i]) . " ";
    echo PHP_EOL;
}

How It Works

1. Prompt and scale. $k = ord($top) - ord('A') sets both phases and both halves.

2. Same diamond core. For top = C you get 5 rows of width 5 (2 * $k + 1).

3. Safer input tip. Prefer:

Safer input
$raw = strtoupper(trim(fgets(STDIN)));
if ($raw === '' || strlen($raw) !== 1 || $raw < 'A' || $raw > 'Z') {
    echo "Enter one letter from A to Z." . PHP_EOL;
    exit;
}
$top = $raw[0];

Example 3 — Helper Function

Often clearer: one function owns the floor rule; another prints a full row so both phases stay thin.

PHP
<?php
function print_cell($alpha, $j, $i) {
    echo ($j > $i ? $alpha[$j] : $alpha[$i]) . " ";
}

function print_row($alpha, $k, $i) {
    for ($j = $k; $j >= 0; $j--)
        print_cell($alpha, $j, $i);
    for ($j = 1; $j <= $k; $j++)
        print_cell($alpha, $j, $i);
    echo PHP_EOL;
}

$k = 4;
$alpha = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

for ($i = $k; $i >= 0; $i--)
    print_row($alpha, $k, $i);

for ($i = 1; $i <= $k; $i++)
    print_row($alpha, $k, $i);

How It Works

1. print_cell owns the rule. $j > $i lives in one place.

2. print_row owns both halves. Left $k..0 and right 1..$k, then echo PHP_EOL.

3. Thin phases. The two outer loops only decide which floors to visit.

Edge Cases & Pitfalls

Check these before calling the solution done.

i = 0

Duplicate center row

If the lower phase starts at 0, the A-center line prints twice. Keep for ($i = 1; $i <= $k; $i++).

Right j = 0

Duplicate center A in a row

The right half of each row must start at 1, same as Program 28.

PHP_EOL inside

Column of letters

If echo PHP_EOL is inside either half-loop, each cell lands on its own line. Use echo for cells; end the row only after both halves.

top = A

Single A

When $k = 0, upper prints A and lower never runs. A good sanity check.

Upper only

Program 28 by mistake

Forgetting the lower phase leaves the open square. Add for ($i = 1; $i <= $k; $i++) with the same row printer.

Bad input

Validate one letter

Trim, uppercase, and require length 1 in A–Z — reject empty or multi-character tokens.

Time and Space Complexity

ProgramTimeExtra space
Two phases inline (Examples 1–2)O(n²)O(1) (plus the fixed alphabet table)
Helper functions (Example 3)O(n²)O(1)

For $n = $k + 1 letters, there are 2n - 1 rows of width 2n - 1 — still quadratic in n. Roughly twice Program 28’s cell count, minus one shared center row.

Key Takeaways

  • Rule: same print_row as Program 28 — upper $k..0, lower 1..$k.
  • No double center: lower phase starts at B ($i = 1).
  • Break the row: call echo PHP_EOL only after both half-loops.
  • Complexity: O(n²) time; O(1) extra space.

One line: print Program 28’s rows from $k down to 0, then again from 1 up to $k.

Frequently Asked Questions

The first loop decreases i from E down to A, printing each layered row toward the center. The second increases i from B back to E with the same row rule so the pyramid widens again without repeating the A-centered row.
Because the A-centered row already appears in the upper half. Starting from B prevents duplicating the center line.
If n is the number of letters from A to the top letter, total rows are 2n−1 (9 rows for A..E).
It prints the border letter when the column index j is above the current row floor i; otherwise it prints the floor letter. The same rule applies on both left and right halves of every row.
The left scan goes E down to A; the right scan goes B up to E so the middle A appears once and the row mirrors.
O(n²) because there are O(n) rows and each row prints O(n) cells.
echo stays on the same line. echo PHP_EOL ends the current line. Letters use echo; the row break uses echo PHP_EOL after both half-loops.
Program 28 is exactly the upper half of this pyramid. Program 29 reuses that row logic, then mirrors upward from B to E for the closed diamond.

Did you know?

Reuse Program 28’s row logic twice: first with i from E down to A, then with i from B up to E so the center row is not duplicated. Each row stays full width (2n-1 cells); total rows are also 2n-1 for n letters.

Next: Decreasing & Increasing Rows

Move from a layered diamond to fixed-width rows with a decreasing prefix and increasing suffix.

Program 30 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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