Shape Rule
0 at center
Each row is ascending digits, 0, then descending digits — a mirror around zero.

The 0-centered descending mirror pattern prints 0, 909, 89098, … up to 1234567890987654321 — a natural step after the palindrome triangle in Program 27. This tutorial covers three nested loops, a fixed zero center, a live preview, algorithm steps, worked PHP examples, edge cases, and complexity.
0 at center
Each row is ascending digits, 0, then descending digits — a mirror around zero.
i = 10..1
for ($i = 10; $i >= 1; $i--) — descending outer loop grows each row.
i..9
for ($j = $i; $j < 10; $j++) prints the ascending left half.
9..i
for ($k = 9; $k >= $i; $k--) mirrors digits after the zero.
max 3–9
Pick a max digit and draw the 0-centered mirror pattern in the browser.
Complexity
Total prints grow as n² for max digit n.
A 0-centered descending mirror number pattern prints ascending digits, a fixed 0, then descending digits on each row. With max digit 9, the output grows from 0 to 1234567890987654321.
In PHP you use a descending outer loop i = 10..1, ascending inner loop j = i..9, print 0, then descending inner loop k = 9..i.
It introduces three coordinated loops with a fixed center — a step up from Program 27’s two-loop palindrome.
echo "0" between both inner loops.
Ascending digits grow as i decreases.
Descending mirror completes each row.
Follow Program 27; continue to Program 29 (spaced mirror) next.
In short: for each i from 10 down to 1, print i..9, then 0, then 9..i, then echo PHP_EOL.
Given max digit 9, print 10 rows of a 0-centered mirror: for each descending i, print i..9, then 0, then 9..i on the same line.
// max = 9 (conceptual shape)
// 0
// 909
// 89098
// 7890987
// ...
// 1234567890987654321 | Item | Type | Description |
|---|---|---|
max | int | Highest digit on each side — typically 9; outer loop starts at max + 1. |
i | int | Descending outer loop — controls how many digits appear on each side. |
j | int | Ascending loop — prints i..max (left half). |
k | int | Descending loop — prints max..i (right half). |
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 | Approach | Idea | Best for |
|---|---|---|
| Three loops + 0 | 0, 909, 89098, … | Learning and interviews |
| Custom max digit | (int) trim(fgets(STDIN)); | Flexible console programs |
| Spaced output | echo $j . " " | Easier reading for wide rows |
| Goal | Pattern |
|---|---|
| Walk rows | for ($i = 10; $i >= 1; $i--) |
| Left half | for ($j = $i; $j < 10; $j++) echo $j; |
| Center zero | echo "0"; |
| Right half | for ($k = 9; $k >= $i; $k--) echo $k; |
| End the row | echo PHP_EOL; |
| Custom max | for ($i = $max + 1; $i >= 1; $i--) with j <= max, k >= i |
Same 0-centered mirror — different ways to control max digit and formatting.
i = max+1..1Descending — grows each row
j = i..maxAscending digits
echo "0"Fixed zero between loops
i = max+1First row prints only 0
Reach for this pattern when teaching three coordinated loops with a fixed center character.
Natural follow-up after Program 27 — introduces a fixed 0 center and descending outer loop.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare Program 27 (palindrome triangle) and Program 29 (spaced mirror) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a max digit between 3 and 9 and draw the 0-centered mirror pattern in the browser.
Three complete PHP programs — fixed max digit, custom max input, and spaced output variant. Click View Output to reveal sample console results.
Print ten rows of the 0-centered mirror with max digit 9 and outer loop i = 10..1.
max = 9Hard-coded max digit — ideal for first demos and screenshots.
<?php
for ($i = 10; $i >= 1; $i--) {
for ($j = $i; $j < 10; $j++) {
echo $j;
}
echo "0";
for ($k = 9; $k >= $i; $k--) {
echo $k;
}
echo PHP_EOL;
} When i = 10, both inner loops are empty — output is just 0. When i = 9, print 9, then 0, then 9 — output 909. When i = 1, the full mirror 1234567890987654321 appears.
Read the max digit with fgets(STDIN) and generalize loop bounds.
Read max with (int) trim(fgets(STDIN)); outer loop runs from max + 1 down to 1.
<?php
echo "Enter max digit (1-9): ";
$max = (int) trim(fgets(STDIN));
if ($max < 1) $max = 1;
if ($max > 9) $max = 9;
for ($i = $max + 1; $i >= 1; $i--) {
for ($j = $i; $j <= $max; $j++) {
echo $j;
}
echo "0";
for ($k = $max; $k >= $i; $k--) {
echo $k;
}
echo PHP_EOL;
} Replace hard-coded 9 and 10 with max and max + 1. Clamp input to 1..9 so loop bounds stay valid. Non-numeric input leaves max unset if you skip is_numeric() checks — always check it in safer labs.
Add a space between digits for easier reading on wide rows.
Keep max = 9 but print each digit followed by a space in both loops.
<?php
$max = 9;
for ($i = $max + 1; $i >= 1; $i--) {
for ($j = $i; $j <= $max; $j++) {
echo $j . " ";
}
echo "0 ";
for ($k = $max; $k >= $i; $k--) {
echo $k . " ";
}
echo PHP_EOL;
} Only the print statements change — echo $j . " " and echo $k . " ". The three-loop structure and 0 center stay the same as Example 1.
Set max digit $max = 9; and use fgets(STDIN) when reading input. Set loop variables $i, $j, $k.
for ($i = 10; $i >= 1; $i--) — descending outer loop; one row per iteration.
for ($j = $i; $j < 10; $j++) — prints digits i..9 (left half).
echo "0" — fixed center between both inner loops.
for ($k = 9; $k >= $i; $k--) — prints digits 9..i, then echo PHP_EOL.
Rows grow toward 1234567890987654321 — O(n²) time, O(1) extra memory.
max = 9 (selected rows)Trace selected outer-loop values of i, the left half, center, right half, and full row output.
i | Left (j) | Center | Right (k) | Row output |
|---|---|---|---|---|
10 | (none) | 0 | (none) | 0 |
9 | 9 | 0 | 9 | 909 |
8 | 8, 9 | 0 | 9, 8 | 89098 |
2 | 2..9 | 0 | 9..2 | 23456789098765432 |
1 | 1..9 | 0 | 9..1 | 1234567890987654321 |
When i = max + 1, both inner loops are empty — only 0 prints. Each row grows as i decreases.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j < 10 to j <= 10 and watch the left half grow differently.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 29 for a spaced mirror with alignment gaps.
Practice echo vs echo PHP_EOL without complex math.
Example: put echo PHP_EOL inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use echo $j . " " in both inner loops.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for max = 5 — grows toward a full mirror row of 11 digits.
Pair the pattern with fgets(STDIN) return checks and positive-row checks.
Example: reject max <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner PHP courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i, j, and k on paper for max = 3 before coding — when i = 4 only 0 prints.
Small habits that keep number-pattern code clean.
Use j < max + 1 or j <= max on the left, and k >= i on the right — match hard-coded 9 and 10 when generalizing.
is_numeric()Call is_numeric(trim($line)) so bad input does not leave max uninitialized.
Only call echo PHP_EOL after the inner loop finishes the row.
Mark the ascending half and mirror half for each row before coding.
Trace i = 4..1 on paper before coding the full max = 9 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put echo PHP_EOL inside the inner loop.
Mistakes that commonly break 0-centered mirror patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use echo $j, echo "0", or echo $k; echo PHP_EOL only after all three parts.
Starting at i = max skips the single-0 first row.
→ Start the outer loop at max + 1 so the first row prints only 0.
Without echo "0", rows concatenate digits with no fixed center.
→ Print 0 between the ascending and descending inner loops.
Using j <= max on the left but k > i on the right breaks symmetry.
→ Mirror bounds: left j = i..max, right k = max..i.
Letters or empty input leave max uninitialized.
→ Call is_numeric(trim($line)) and re-prompt on failure.
Check these inputs before calling the solution done.
Both inner loops empty — output is just 0.
Clamp or reject — loops need a positive max digit.
Single-digit pattern — clamp to 9 for console demos.
Two rows: 0 and 101.
Unchecked fgets(STDIN) input leaves max unset — call is_numeric(trim($line)) first.
Output grows as max² digits — fine for labs, noisy beyond 9.
Try these variations to lock in the pattern.
0 with * or #max = 4 and trace every row0 between the ascending loop (j) and descending loop (k) on every row.echo stays on the line; echo PHP_EOL advances — mix them carefully.max in 1..9 for interactive programs; max = 1 gives rows 0 and 101.echo $j . " " in both loops for easier reading on wide rows.Quick Takeaway: outer loop $i = $max+1..1, ascending $j = $i..$max, echo "0", descending $k = $max..$i, then echo PHP_EOL.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Spaced output (Example 3) | O(n²) | O(1) |
The 0-centered descending mirror pattern is a compact lesson in three coordinated loops: print ascending i..max, a fixed 0, then descending max..i. Master the fixed-max = 9 version, then try custom max and spaced output.
Practice the three examples above, then continue to Program 29 for the spaced mirror number pattern.
Start the outer loop at max + 1 for the single-0 first row — validate max when reading from the console.
for ($i = $max + 1; $i >= 1; $i--) in the outer loopfor ($j = $i; $j <= $max; $j++)echo "0" between inner loopsfor ($k = $max; $k >= $i; $k--)is_numeric(trim($line)) and clamp max to 1..9echo PHP_EOL inside either inner loopmax instead of max + 10 between loopsj and ki = max + 1 edge casePrint the pattern the beginner-friendly way.
j..max, 0, max..i
DefinitionLeft half
CodeCenter
CodeRight half
ShapeO(n²) time
AnalysisThis pattern prints ascending digits from $i to 9, a fixed 0 in the center, then descending digits from 9 down to $i. As $i decreases, each row grows into the long mirror 1234567890987654321.
Move on to the spaced mirror number pattern in the PHP number-pattern series.
12 people found this page helpful