PHP Alphabet Pyramid Pattern (Centered)

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

What Is This Pattern?

A centered alphabet pyramid prints odd-width rows (1, 3, 5, …) with leading spaces for centering, while letters advance continuously across the whole triangle — A, then B C D, then E F G H I.

Remember
Rule: pad spaces + next odd count of letters (running $k)

    A
  B C D
E F G H I     ← 3 rows (bottom width 5)

Unlike Program 14 (each row restarts at A), here one counter walks the alphabet across every row. Leading spaces keep each line under the widest bottom row.

How to Solve It

Step odd row widths, pad with spaces, then print the next letters from a running counter.

MethodIdeaBest for
Scan columnsInner loop walks bottom width; print space or next letterLearning one-loop centering
Explicit padSeparate pad loop, then letter loopClearer reading; row-count input

Pseudocode

Pseudocode
$k = 'A'
$width = 2 * $rows - 1
for $i from 1 to $width step 2:
    for $j from $width down to 1:
        if $j > $i:
            echo space
        else:
            echo $k and a space
            $k = next letter
    echo PHP_EOL

Cheat sheet

GoalPattern
Odd row widthsfor ($i = 1; $i <= $width; $i += 2)
Scan bottom widthfor ($j = $width; $j >= 1; $j--)
Pad or letterif ($j > $i) echo " "; else echo $k . " ";
Advance letter$k++; (or index into an alphabet array)
Explicit pad$pad = $width - $letters; then echo pad spaces
End the rowecho PHP_EOL;
A–Y-safe heightMax 5 rows (1+3+…+9 = 25 letters)

Printing Letters vs Starting a New Line

APIEffectUse for
echoStays on the same lineEach space and each letter (often with a trailing space)
echo PHP_EOLEnds the current lineAfter the column scan / letter loop

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

Live Preview

Change the row count and the centered pyramid updates instantly — including bottom width and letter totals.

Whole numbers from 1 to 5. Bottom width is 2 × rows - 1; total letters equal rows² (stays within A–Y).

Live result 3 rows · width 5 · 9 letters
    A
  B C D
E F G H I

Worked Walkthrough — $rows = 3 (width 5)

Trace each odd width, how many pads print, and which letters the running counter emits.

RowWidth $iPadsLettersPrinted row
114AA
232B C DB C D
350E F G H IE F G H I

Total letters: 1 + 3 + 5 = 9 = 3². The counter never resets — it keeps climbing past each row.

PHP Programs

Three complete programs: fixed width 5, odd-width input, and an explicit pad-then-letters form. Use View Output to reveal sample results.

Example 1 — Fixed bottom width 5

Hard-coded bounds — ideal for first demos and screenshots.

PHP
<?php
$k = 0;
$alpha = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

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

How It Works

1. Outer loop steps odd widths. $i takes 1, 3, 5 — the letter count for each row.

2. Inner loop scans the bottom width. While $j > $i, echo a pad space; otherwise echo the next alphabet letter plus a separator space.

3. Running index. $k never resets, so letters flow A → B C D → E F G H I across the pyramid.

Example 2 — Odd Width Input

Read the bottom width as an odd number (like 5 or 7). Prefer is_numeric and odd validation in real apps.

PHP
<?php
echo "Enter the bottom width (odd number): ";
$width = (int) trim(fgets(STDIN));

$k = 'A';
for ($i = 1; $i <= $width; $i += 2) {
    for ($j = $width; $j >= 1; $j--) {
        if ($j > $i) {
            echo " ";
        } else {
            echo $k . " ";
            $k++;
        }
    }
    echo PHP_EOL;
}
?>

How It Works

1. Same centering scan. Only the outer/inner bounds follow $width instead of the literal 5.

2. Char counter. $k starts at 'A' and increments after each printed letter.

3. Safer input tip. Require a positive odd width and stay within A–Z:

Safer input
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
    echo "Enter an odd width from 1 to 9." . PHP_EOL;
    exit;
}
$width = (int) $input;
if ($width < 1 || $width % 2 === 0 || $width > 9) {
    echo "Enter an odd width from 1 to 9." . PHP_EOL;
    exit;
}

Example 3 — Pad Spaces, Then Letters

Often clearer to read: print leading spaces first, then the odd letter count.

PHP
<?php
$rows = 3;
$width = 2 * $rows - 1;
$k = 'A';

for ($row = 1; $row <= $rows; $row++) {
    $letters = 2 * $row - 1;
    $pad = $width - $letters;

    for ($s = 0; $s < $pad; $s++) {
        echo " ";
    }

    for ($L = 0; $L < $letters; $L++) {
        echo $k;
        if ($L < $letters - 1) echo " ";
        $k++;
    }

    echo PHP_EOL;
}
?>

How It Works

1. Map row → letters. Row $r needs 2*$r - 1 letters and $width - $letters leading spaces.

2. Two clear loops. First pad, then echo letters with separators between them — same visual pyramid as the scan version.

3. Row-count friendly. Driving from $rows makes $width = 2 * $rows - 1 automatic.

Edge Cases & Pitfalls

Check these before calling the solution done.

Even width

Broken pyramid

An even bottom width never lands on a clean 1, 3, 5, … stack. Require an odd positive width.

Reset $k

Wrong letters

Do not reset the letter counter each row. Resetting turns this into a different pattern (fresh A on every line).

No pads

Left-aligned triangle

Skipping the space branch left-aligns the odd rows — you lose the centered pyramid look.

PHP_EOL early

Broken row

Call echo PHP_EOL only after the column scan. Inside the inner loop, each cell lands on its own line.

$rows = 1

Single A

Output is just A — a good sanity check.

Past Z

Clamp to 5 rows

Five rows use 25 letters (A–Y). Six rows need 36 — past Z. Cap height or wrap with care.

Time and Space Complexity

ProgramTimeExtra space
Scan / explicit pad formsO(r²)O(1)

Each of r rows scans about 2r columns (or prints O(r) pads and letters). Total letters printed equal r².

Key Takeaways

  • Odd widths: outer loop steps 1, 3, 5, … under a matching bottom width.
  • Center with pads: echo spaces while the column is outside the current row width.
  • Running counter: one $k walks the alphabet across every row — do not reset it.
  • Complexity: O(r²) time because total letters equal r²; O(1) extra space.

One line: for each odd width, pad spaces then echo the next letters from a continuous counter.

Frequently Asked Questions

The inner loop scans a fixed bottom width. While the column index is still outside the current row width, it prints spaces; otherwise it prints the next letter.
The outer loop increases by 2 (1, 3, 5), so each row prints an odd number of letters, forming a pyramid shape.
Increase the maximum odd width (and loop bounds). The number of rows grows as width grows: 1, 3, 5, 7...
They shift each row to the right so the odd-width lines are centered under the widest line.
The trailing space makes columns easier to see. If you want a compact output, echo just $k (or control separators carefully).
echo sends output without adding a line break. echo PHP_EOL ends the current line with a portable newline. Spaces and letters use echo; the row break uses echo PHP_EOL after the inner scan.
O(r²) for r rows because each row scans a fixed-width set of columns and prints a growing number of characters overall.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int, and require an odd positive width (1, 3, 5, …). Cap so total letters 1+3+…+width stay within A–Z if you want only alphabetic output.

Did you know?

This pattern combines two ideas: odd-width rows (i += 2) and centering via padding spaces (like star pyramids). Letters flow continuously via one counter — A, then B C D, then E F G H I — while leading spaces keep each row aligned under the widest line.

Next: Reverse Diagonal Star

Descending letters with a star marking the diagonal.

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