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.
Approach
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.
Method
Idea
Best for
Three inner loops
1..$i, then ** pairs, then $i..1
Learning, interviews, exams
Custom fill
Same 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
Goal
Pattern
Outer (descend)
for ($i = $n; $i >= 1; $i--)
Left digits
for ($j = 1; $j <= $i; $j++) echo $j;
Star center
for ($k = $i; $k < $n; $k++) echo "**";
Right digits
for ($m = $i; $m >= 1; $m--) echo $m;
End the row
echo PHP_EOL;
Custom fill
Print "##" (or " ") instead of "**"
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j / echo "**"
Stays on the same line
Each digit and each ** pair
echo PHP_EOL
Ends the current line
After all three inner loops
Print the whole row without a newline, then end the row once.
Try it
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.
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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²).
Remember
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.