Mirror Numbers with Growing Asterisks in PHP

What You’ll Learn
How to print a mirrored number pattern in PHP where the middle section becomes asterisks as rows go down:
1234554321 → 1********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:
1234554321
1234**4321
123****321
12******21
1********1Complete PHP Program
We print 1..$i, then print ** pairs, then print $i..1 to mirror the left side.
<?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
Choose n
$n = 5; controls the starting width.
Outer loop (decrease i)
for ($i = $n; $i >= 1; $i--) shrinks the number part and grows the middle asterisks.
Left side: print 1..i
The first inner loop prints ascending digits from 1 up to $i.
Middle: print ** pairs
The middle loop runs $n - $i times and prints "**" each time.
Right side: print i..1
The final loop prints descending digits from $i down to 1 to mirror the left side.
Mirrored pattern
Numbers shrink symmetrically while the middle grows with asterisks, keeping the line visually balanced.
Variation — User Input (CLI) Version
Let the user choose $n at runtime (run from terminal with 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
$nso 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
Use multiple inner loops to build a single line (left, middle, right).
The left and right loops mirror each other: 1..i and i..1.
The middle grows as $n - $i increases while $i decreases.
For larger $n, add spacing or padding for readability.
❓ Frequently Asked Questions
$i decreases from $n to 1, so $n - $i increases and we print more ** pairs.echo "**"; to any string you want (for example, "--" or "##")."**" with "*". If you want to keep the same total width, you may need to print twice as many * characters.Explore More PHP Number Patterns!
Try combining symmetry and fill characters to create diamonds, frames, and hollow patterns.
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.
12 people found this page helpful
