A 0-centered descending mirror prints ascending digits $i..$max, a fixed 0, then descending $max..$i — each row mirrors around zero.
Remember
Rule: for i from max+1 down to 1
print j from i to max
print 0
print k from max down to i
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321 ← max = 9
Follows the palindrome triangle in Program 27; next is the spaced mirror in Program 29.
Approach
How to Solve It
Outer loop decreases $i from $max + 1 to 1. Print left half, then 0, then the mirror half.
Method
Idea
Best for
Three loops + fixed 0
Ascend $i..$max, print 0, descend $max..$i
Learning, interviews, exams
Spaced digits
Same loops with echo $j . " "
Easier reading for larger max
Pseudocode
Pseudocode
for i from max + 1 down to 1:
for j from i to max:
print j
print 0
for k from max down to i:
print k
print newline
Cheat sheet
Goal
Pattern
Shrink start digit
for ($i = $max + 1; $i >= 1; $i--)
Left ascending
for ($j = $i; $j <= $max; $j++) echo $j;
Fixed center
echo "0";
Right mirror
for ($k = $max; $k >= $i; $k--) echo $k;
End of row
echo PHP_EOL;
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j / $k / "0"
Stays on the same line
Each digit on the row
echo PHP_EOL
Ends the current line
After both half-loops and the center finish
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the max digit and the 0-centered mirror updates instantly — capped at 9 for readable demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultmax = 5 · 36 digits
0
505
45054
3450543
234505432
12345054321
Trace
Worked Walkthrough — $max = 4
Trace how left half, center 0, and right half build each row as $i decreases.
$i
Left
Center
Right
Prints
5
(none)
0
(none)
0
4
4
0
4
404
3
34
0
43
34043
2
234
0
432
2340432
1
1234
0
4321
123404321
There are $max + 1 rows. Digit total = (max + 1)² → O(n²) for max digit n.
Code
PHP Programs
Three complete programs: fixed $max = 9, CLI input, and a compact $max = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed $max = 9
Hard-coded max digit — ascend $i..9, print 0, then mirror 9..$i.
1. Four rows.$i = 4 → 0; $i = 3 → 303; then 23032 and 1230321.
2. Trace on paper. If you forget echo "0", the halves glue together with no center.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for max 9 or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
no 0
Missing center
Without echo "0" the halves join with no pivot — keep the center print between both loops.
i = max
Skipped first row
Starting at $i = $max skips the lone 0 row. Outer loop must begin at $max + 1.
PHP_EOL inside
Broken rows
If echo PHP_EOL sits inside either half-loop, the mirror splits across lines. Call it only after the full row.
max = 1
Smallest mirror
Output is 0 then 101 — a good sanity check for bounds and the center print.
max > 9
Multi-digit glue
Values above 9 print multi-digit numbers that run together. Cap demos or add spaces.
fgets
Check is_numeric
Bare (int) casts turn non-numeric strings into 0 — prefer is_numeric before casting.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact max = 3 (Example 3)
O(n²)
O(1)
There are n + 1 rows for max digit n. Digit total = (n + 1)² → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Three parts: print $i..$max, then 0, then $max..$i.
Start at max+1: the first row is only 0; then both halves grow.
Break the row: digits and center with no newline; echo PHP_EOL after the full row.
Complexity:O(n²) time from (n+1)² digits; O(1) extra space.
One line: for each $i from $max+1 down to 1, print $i..$max, then 0, then $max..$i, then echo PHP_EOL.
Frequently Asked Questions
A 0-centered descending mirror: for max=9 you get 0 / 909 / 89098 / … / 1234567890987654321 — each row mirrors around a fixed zero.
echo "0" sits between the ascending and descending loops, creating a fixed center on every row.
When $i = $max + 1, both side loops are empty — only 0 is printed.
Starting at $max + 1 gives the single-0 row first; then $i decreases and both halves grow toward $max.
Program 27 mirrors 1..$i on each row. Program 28 uses a fixed 0 center and grows digits toward $max on both sides as $i decreases.
Use echo $j . " " and echo $k . " " in the loops instead of echo $j.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int, then clamp $max to 1..9 — see Example 2.
O(n²) for max digit n because there are n+1 rows and the digit total is (n+1)².
🤔
Did you know?
This pattern prints ascending digits from $i to $max, a fixed 0 in the center, then descending digits from $max down to $i. As $i decreases, each row grows into the long mirror 1234567890987654321.