PHP Number Mirror Pattern (Asterisk Center)

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

What Is This Pattern?

A number & asterisk mirror prints ascending digits, a growing star center, then descending digits on each row — left and right halves mirror each other.

Remember
Rule: for i from n down to 1,
      print 1..i, then (n - i) pairs of **, then i..1

1234554321
1234**4321
123****321
12******21
1********1     ← n = 5

In PHP three inner loops do the work: ascending ($j), star pairs ($k), descending ($m), then echo PHP_EOL ends the row. Program 24 continues with a centered continuous pyramid.

How to Solve It

Outer loop counts down from $n to 1. Print 1..$i, then $n - $i pairs of **, then $i..1. End each row with echo PHP_EOL.

MethodIdeaBest for
Three inner loops1..$i, then ** pairs, then $i..1Learning, interviews, exams
Custom fillSame loops; print ## (or spaces) instead of **When the center symbol should change

Pseudocode

Pseudocode
for i from n down to 1:
    for j from 1 to i:
        print j
    for k from i to n - 1:
        print "**"
    for m from i down to 1:
        print m
    print newline

Cheat sheet

GoalPattern
Outer (descend)for ($i = $n; $i >= 1; $i--)
Left digitsfor ($j = 1; $j <= $i; $j++) echo $j;
Star centerfor ($k = $i; $k < $n; $k++) echo "**";
Right digitsfor ($m = $i; $m >= 1; $m--) echo $m;
End the rowecho PHP_EOL;
Custom fillPrint "##" (or " ") instead of "**"

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $j / echo "**"Stays on the same lineEach digit and each ** pair
echo PHP_EOLEnds the current lineAfter all three inner loops

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

Live Preview

Change the size n and the mirror pattern updates instantly — capped at 9 so every digit stays a single character.

Whole numbers from 1 to 9 (single digits). Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · 5 rows
1234554321
1234**4321
123****321
12******21
1********1

Worked Walkthrough

Trace $n = 4 — left digits, star pairs, and right digits for each row.

$iLeftStarsRightPrinted row
412340432112344321
31231 × **321123**321
2122 × **2112****21
113 × **11******1

Star pairs per row: $n - $i. Every row has the same total width once digits and ** pairs are counted — that is why time is O(n²).

PHP Programs

Three complete programs: fixed size, CLI input, and a custom center fill. Use View Output for sample results.

Example 1 — Fixed $n = 5

Hard-coded size — left digits, ** center, right digits.

PHP
<?php
$n = 5;

for ($i = $n; $i >= 1; $i--) {
    for ($j = 1; $j <= $i; $j++) {
        echo $j;
    }

    for ($k = $i; $k < $n; $k++) {
        echo "**";
    }

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

    echo PHP_EOL;
}
?>

How It Works

1. Outer shrinks. $i runs from $n down to 1 — fewer digits and more stars each row.

2. Left then right. First loop prints 1..$i; third loop prints $i..1 to mirror it.

3. Growing center. The middle loop runs $n - $i times, printing ** each time.

Example 2 — User Input

Read $n from the CLI. Prefer is_numeric in real apps (shown in the tip below).

PHP
<?php
echo "Enter the size: ";
$n = (int) trim(fgets(STDIN));

for ($i = $n; $i >= 1; $i--) {
    for ($j = 1; $j <= $i; $j++) {
        echo $j;
    }

    for ($k = $i; $k < $n; $k++) {
        echo "**";
    }

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

    echo PHP_EOL;
}
?>

How It Works

1. Read size. Ask for $n, then trim(fgets(STDIN)) and cast to int.

2. Same three loops. Star count still equals $n - $i for each row.

3. Safer input tip. Bare (int) casts turn letters into 0. Prefer:

Safer input
echo "Enter the size: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
    echo "Enter a positive whole number." . PHP_EOL;
    exit(1);
}
$n = (int) $input;

Example 3 — Custom Fill ##

Keep $n = 5 but use hash pairs instead of asterisks in the center.

PHP
<?php
$n = 5;

for ($i = $n; $i >= 1; $i--) {
    for ($j = 1; $j <= $i; $j++) {
        echo $j;
    }

    for ($k = $i; $k < $n; $k++) {
        echo "##";
    }

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

    echo PHP_EOL;
}
?>

How It Works

1. Same structure. Outer and digit loops match Example 1.

2. Different center. Only the middle loop changes — "##" instead of "**".

3. Same width. Any two-character fill keeps the row length consistent.

Edge Cases & Pitfalls

Check these before calling the solution done.

single *

Printing "*" instead of "**"

The center grows too slowly and symmetry breaks. Keep two characters per star-loop iteration.

wrong bound

$k <= $n instead of $k < $n

That prints one extra ** pair. Use for ($k = $i; $k < $n; $k++) for exactly $n - $i pairs.

PHP_EOL inside

echo PHP_EOL inside an inner loop

That splits the mirror across lines. Call echo PHP_EOL only after all three loops finish.

n = 1

Single pair

Output is 11 — left 1 and right 1, no stars.

n ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

multi-digit

$n > 9

echo 10 prints two characters and breaks visual alignment. Labs usually stick to 1–9.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input / custom fill (Examples 1–3)O(n²)O(1)

There are n rows, and each row prints O(n) characters (digits plus star pairs), so total work is O(n²).

Key Takeaways

  • Rule: for $i = n..1, print 1..$i, then (n - i) pairs of **, then $i..1.
  • Three loops: left digits, center fill, right digits — that is the whole mirror.
  • echo vs PHP_EOL: digits and stars stay on the line; echo PHP_EOL advances after all three loops.
  • Next step: Program 24 builds a centered continuous number pyramid.

One line: descend $i, print 1..$i, fill with **, mirror with $i..1, then echo PHP_EOL.

Frequently Asked Questions

Each iteration prints two asterisks so the center grows by 2 characters per row while keeping the pattern symmetric.
The pattern prints ascending numbers 1..$i, then stars, then descending numbers $i..1 on the same line.
Because $i starts at $n and decreases — each row prints fewer digits and more stars in the center.
echo $j stays on the same line. echo PHP_EOL ends the current line. Digits and stars use echo; the row break uses echo PHP_EOL after all three inner loops.
Three — ascending digits ($j), star pairs ($k), and descending digits ($m).
Yes. Replace "**" with "##", two spaces, or any fill string (see Example 3).
O(n²) for size n because each of n rows prints O(n) characters overall.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int so bad input does not produce unexpected results.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Each row prints 1..$i, then a growing block of ** pairs, then $i..1 — three inner loops create a symmetric mirror. As $i shrinks, the star block grows to keep row width consistent.

Next: Centered Continuous Pyramid

Continue with a continuous counter shaped into a centered pyramid.

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