PHP Alphabet Pattern (Symmetric with Star Center)

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

What Is This Pattern?

A symmetric alphabet with star center prints left letters A..$i, an even star gap, then mirrored letters $i..A. Letter halves shrink while stars grow, so every row keeps the same width 2n.

Remember
Rule: left A..i + 2*(top-i) stars + right i..A

ABCDEEDCBA
ABCD**DCBA
ABC****CBA
AB******BA
A********A     ← 5 rows (top = E)

On the first row the star count is 0, so the middle letter appears twice (EE in ABCDEEDCBA). That is intentional — left ends at $i and right starts at $i.

How to Solve It

Count down the letter-half end, then print three parts in order: left, stars, right.

MethodIdeaBest for
Three nested loopsLeft letters, star loop, right lettersLearning, interviews, exams
str_repeat shortcutSame letters; one call for the star gapCleaner demos once the shape is clear

Pseudocode

Pseudocode
$top = 'A' + $rows - 1
for $i from $top down to 'A':
    for $j from 'A' to $i:
        echo $j (no newline)
    $stars = 2 * (ord($top) - ord($i))
    echo $stars copies of '*'
    for $m from $i down to 'A':
        echo $m (no newline)
    echo PHP_EOL

Cheat sheet

GoalPattern
Pick the letter-half endfor ($i = $top; $i >= 'A'; $i--)
Left ascendingfor ($j = 'A'; $j <= $i; $j++) echo $j;
Star gap$stars = 2 * (ord($top) - ord($i));
Right descendingfor ($m = $i; $m >= 'A'; $m--) echo $m;
One-call starsecho str_repeat("*", $stars);
End the rowecho PHP_EOL;
A–Z-safe heightClamp $rows to 1–26

Printing Letters vs Starting a New Line

APIEffectUse for
echoStays on the same lineEach letter and each star
echo PHP_EOLEnds the current lineAfter left + stars + right

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

Live Preview

Change the row count and the symmetric star-center pattern updates instantly — including top letter and width.

Whole numbers from 1 to 13. Top letter is A + rows - 1; each row is width 2 × rows.

Live result 5 rows · top E · width 10
ABCDEEDCBA
ABCD**DCBA
ABC****CBA
AB******BA
A********A

Worked Walkthrough — $top = 'E'

Trace each letter-half end, the star gap, and the full fixed-width row.

$iLeftStarsRightPrinted row
EABCDE0EDCBAABCDEEDCBA
DABCD2DCBAABCD**DCBA
CABC4CBAABC****CBA
BAB6BAAB******BA
AA8AA********A

Every row is width 10 = 2 × 5. Letters shrink by 1 on each side; stars grow by 2.

PHP Programs

Three complete programs: fixed $top = 'E', CLI row count, and a str_repeat star shortcut. Use View Output to reveal sample results.

Example 1 — Fixed $top = 'E'

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

PHP
<?php
$top = 'E';

for ($i = $top; $i >= 'A'; $i--) {
    for ($j = 'A'; $j <= $i; $j++) {
        echo $j;
    }

    $stars = 2 * (ord($top) - ord($i));
    for ($s = 1; $s <= $stars; $s++) {
        echo "*";
    }

    for ($m = $i; $m >= 'A'; $m--) {
        echo $m;
    }

    echo PHP_EOL;
}
?>

How It Works

1. Outer countdown. $i walks E, D, C, B, A — the end of each letter half.

2. Left then stars then right. Print A..$i, then 2*(ord($top) - ord($i)) stars, then $i..A.

3. Fixed width. When $i = 'E', stars = 0 and the doubled E appears. Later rows shrink letters and grow stars, keeping width 10.

Example 2 — User Input Version

Compute $top = chr(ord('A') + $rows - 1), then reuse the same three-part row. Prefer is_numeric in real apps.

PHP
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));

$top = chr(ord('A') + $rows - 1);

for ($i = $top; $i >= 'A'; $i--) {
    for ($j = 'A'; $j <= $i; $j++) {
        echo $j;
    }

    $stars = 2 * (ord($top) - ord($i));
    for ($s = 1; $s <= $stars; $s++) {
        echo "*";
    }

    for ($m = $i; $m >= 'A'; $m--) {
        echo $m;
    }

    echo PHP_EOL;
}
?>

How It Works

1. Map rows → top. For $rows = 4, $top is 'D' and each row has width 8.

2. Same three-part core. Only the top letter changes — left, stars, and right match Example 1.

3. Safer input tip. Prefer is_numeric and clamp to A–Z:

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

Example 3 — str_repeat("*", $stars)

Keep the letter loops; build the center gap in one call.

PHP
<?php
$top = 'E';

for ($i = $top; $i >= 'A'; $i--) {
    for ($j = 'A'; $j <= $i; $j++) {
        echo $j;
    }

    $stars = 2 * (ord($top) - ord($i));
    echo str_repeat("*", $stars);

    for ($m = $i; $m >= 'A'; $m--) {
        echo $m;
    }

    echo PHP_EOL;
}
?>

How It Works

1. Same left and right. Letter halves are unchanged from Example 1.

2. One-call gap. str_repeat("*", $stars) creates the whole even star gap at once.

3. When to use which. Keep the explicit star loop for exams that ask you to show all bounds; use str_repeat for cleaner demos.

Edge Cases & Pitfalls

Check these before calling the solution done.

Order

Wrong part order

Always print left, then stars, then right. Swapping sides breaks the mirror.

stars without 2*

Odd gap

Forget the 2 * and the gap is too small — width will not stay 2n.

Right from $i-1

Missing middle twin

Starting the right half at chr(ord($i) - 1) removes the doubled center letter. Only do that if the problem asks for it.

PHP_EOL early

Broken row

Call echo PHP_EOL only after all three parts. Inside any inner loop, the row splits.

$rows = 1

Single AA

Output is AA (left A + right A, zero stars) — a good sanity check.

Past Z

Clamp to 26

More than 26 rows walks past Z. Clamp or reject the input.

Time and Space Complexity

ProgramTimeExtra space
Three-loop / str_repeat formsO(n²)O(1) loops; O(n) for the string gap

Each of n rows prints 2n characters (letters + stars), so total work is quadratic in the row count.

Key Takeaways

  • Three parts: left A..$i, even star gap, right $i..A.
  • Even stars: $stars = 2 * (ord($top) - ord($i)) keeps width 2n.
  • Doubled center: first row has zero stars, so the middle letter appears twice.
  • Complexity: O(n²) time; loops use O(1) extra space.

One line: for each end letter counting down, print A..$i, then 2*(ord($top)-ord($i)) stars, then $i..A.

Frequently Asked Questions

The left half prints A..E, and the right half prints E..A. When there are zero stars on the first row, E appears as the last character of the left half and the first character of the right half.
Stars per row are 2*(ord($top) - ord($end)). For top=E: 0, 2, 4, 6, 8 stars as end goes E, D, C, B, A.
Because it is computed as 2*(ord($top) - ord($i)), which is always a multiple of 2.
Yes. You can start the right half from chr(ord($i) - 1) instead of $i when there are zero stars, so the first row becomes ABCDEDCBA.
echo sends output without adding a line break. echo PHP_EOL ends the current line with a portable newline. Letters and stars use echo; the row break uses echo PHP_EOL after all three parts.
O(n²) for n rows. Each row prints a total of 2n characters (letters + stars), repeated across n rows.
Yes. echo str_repeat("*", $stars) prints the whole gap in one call. Nested star loops are fine for learning; str_repeat is a handy shortcut later.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int, then clamp rows between 1 and 26 so bad input does not walk past Z.

Did you know?

Each row is: ascending letters from A to the row end, then 2*(ord($top) - ord($end)) stars, then descending letters back to A. The middle letter appears twice when star count is 0, producing ABCDEEDCBA on the first row. Total width stays constant at 2n characters per row.

Next: Centered Alphabet Pyramid

Leading spaces plus a running letter counter in a pyramid.

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