Mirror Numbers with Growing Asterisks in PHP

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a mirrored number pattern in PHP where the middle section becomes asterisks as rows go down:

12345543211********1

This is a good exercise for combining multiple inner loops to build one line: ascending numbers, asterisks, then descending numbers.

⭐ Pattern Output

For $n = 5, the pattern looks like this:

Output
1234554321
1234**4321
123****321
12******21
1********1
1

Complete PHP Program

We print 1..$i, then print ** pairs, then print $i..1 to mirror the left side.

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

Choose n

$n = 5; controls the starting width.

Setup
2

Outer loop (decrease i)

for ($i = $n; $i >= 1; $i--) shrinks the number part and grows the middle asterisks.

Row control
3

Left side: print 1..i

The first inner loop prints ascending digits from 1 up to $i.

Ascending
4

Middle: print ** pairs

The middle loop runs $n - $i times and prints "**" each time.

Middle fill
5

Right side: print i..1

The final loop prints descending digits from $i down to 1 to mirror the left side.

Mirroring
=

Mirrored pattern

Numbers shrink symmetrically while the middle grows with asterisks, keeping the line visually balanced.

2

Variation — User Input (CLI) Version

Let the user choose $n at runtime (run from terminal with php):

PHP
<?php
echo "Enter n (e.g., 5): ";
$n = (int) trim(fgets(STDIN));

if ($n < 1) {
    echo "Please enter a positive number." . PHP_EOL;
    exit(1);
}

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;
}

💡 Tips for Enhancement

Try These

  • Replace ** with another symbol (like ##)
  • Use * instead of ** for a tighter middle
  • Add spaces between numbers to improve readability for $n > 9
  • Build each row as a string and print once for cleaner output control
  • Center-align the output by adding leading spaces

Avoid

  • Forgetting to keep the middle symmetric (changing the middle affects width)
  • Using $n so large that digits become multi-character without spacing
  • Skipping the final mirrored loop (right side won’t match)
  • Forgetting the newline after each row

Key Takeaways

1

Use multiple inner loops to build a single line (left, middle, right).

2

The left and right loops mirror each other: 1..i and i..1.

3

The middle grows as $n - $i increases while $i decreases.

4

For larger $n, add spacing or padding for readability.

❓ Frequently Asked Questions

Because $i decreases from $n to 1, so $n - $i increases and we print more ** pairs.
Yes—change echo "**"; to any string you want (for example, "--" or "##").
Replace "**" with "*". If you want to keep the same total width, you may need to print twice as many * characters.
O(n²) for n rows, since each row prints O(n) characters across multiple loops.

Explore More PHP Number Patterns!

Try combining symmetry and fill characters to create diamonds, frames, and hollow patterns.

All Number Patterns →
Did you know?

Building a line from multiple parts (prefix, middle, suffix) is the same idea used in many string and formatting problems—patterns are great practice for that mindset.

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