A hollow number diamond is Program 57’s hollow pyramid for rows 1..$n, then the same edge rows mirrored from $n − 1 down to 1 — total 2*$n − 1 lines.
Remember
Rule: for i = 1..n → hollow row i
for i = n-1..1 → hollow row i (mirror)
each row: left j=n..1, right k=2..n (edges only)
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1 ← n = 5 (9 rows)
Unlike Program 57 (top half only), here a second outer loop closes the diamond without repeating the widest row. Program 59 continues with a hollow square border.
Approach
How to Solve It
Print hollow edge rows upward (1..$n), then mirror downward ($n − 1..1). Same left/right edge loops as Program 57 on every row.
Method
Idea
Best for
Fixed n
Hard-code size; top loop + bottom mirror
Labs and demos
CLI input
Same logic; read $n at runtime
Interactive practice
printRow helper
One function for both halves
Less duplicated code
Pseudocode
Pseudocode
for i from 1 to n:
print hollow edge row i
for i from n-1 down to 1:
print hollow edge row i
hollow edge row i:
for j from n down to 1:
if i == j: print j else print space
for k from 2 to n:
if i == k: print k else print space
print newline
Cheat sheet
Goal
Pattern
Set size
$n = 5;
Top half
for ($i = 1; $i <= $n; $i++)
Bottom half
for ($i = $n - 1; $i >= 1; $i--)
Left / right edges
Same as Program 57: $j = $n..1, $k = 2..$n
End the row
echo PHP_EOL;
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j / echo " "
Stays on the same line
Each edge digit or space
echo PHP_EOL
Ends the current line
After both edge loops finish
Build the full hollow row with echo, then break once.
Try it
Live Preview
Change the half-height and the hollow diamond updates instantly.
Whole numbers from 3 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · rows = 9
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1
Trace
Worked Walkthrough
Trace $n = 5 — how the two outer loops build nine hollow rows without doubling the middle.
Half
Outer loop
Rows printed
Top
$i = 1..5
1 → 2 2 → … → 5 5
Bottom
$i = 4..1
4 4 → … → 1
Start the mirror at $n − 1, not $n — otherwise the widest row prints twice.
Code
PHP Programs
Three complete programs: fixed $n = 5, CLI input, and a printRow helper. Use View Output for sample results.
Example 1 — Fixed $n = 5
Hard-coded size — top half 1..$n, bottom half $n − 1..1, same edge loops per row.