PHP Hollow Number Diamond Pattern

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

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.

MethodIdeaBest for
Fixed nHard-code size; top loop + bottom mirrorLabs and demos
CLI inputSame logic; read $n at runtimeInteractive practice
printRow helperOne function for both halvesLess 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

GoalPattern
Set size$n = 5;
Top halffor ($i = 1; $i <= $n; $i++)
Bottom halffor ($i = $n - 1; $i >= 1; $i--)
Left / right edgesSame as Program 57: $j = $n..1, $k = 2..$n
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $j / echo " "Stays on the same lineEach edge digit or space
echo PHP_EOLEnds the current lineAfter both edge loops finish

Build the full hollow row with echo, then break once.

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 result n = 5 · rows = 9
    1    
   2 2   
  3   3  
 4     4 
5       5
 4     4 
  3   3  
   2 2   
    1    

Worked Walkthrough

Trace $n = 5 — how the two outer loops build nine hollow rows without doubling the middle.

HalfOuter loopRows printed
Top$i = 1..51 → 2 2 → … → 5 5
Bottom$i = 4..14 4 → … → 1

Start the mirror at $n − 1, not $n — otherwise the widest row prints twice.

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.

PHP
<?php
$n = 5;

for ($i = 1; $i <= $n; $i++) {
    for ($j = $n; $j >= 1; $j--) {
        if ($i == $j) echo $j;
        else echo " ";
    }
    for ($k = 2; $k <= $n; $k++) {
        if ($i == $k) echo $k;
        else echo " ";
    }
    echo PHP_EOL;
}

for ($i = $n - 1; $i >= 1; $i--) {
    for ($j = $n; $j >= 1; $j--) {
        if ($i == $j) echo $j;
        else echo " ";
    }
    for ($k = 2; $k <= $n; $k++) {
        if ($i == $k) echo $k;
        else echo " ";
    }
    echo PHP_EOL;
}
?>

How It Works

1. Top half. First outer loop prints the hollow pyramid — same edge logic as Program 57.

2. Bottom half. Second outer loop runs $i from $n − 1 down to 1 with identical edges.

3. No double middle. Starting at $n − 1 keeps the widest row (5 5) once.

Example 2 — User Input

Read $n from the CLI. Prefer is_numeric (shown below).

PHP
<?php
echo "Enter n: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
    echo "Enter a positive whole number." . PHP_EOL;
    exit(1);
}
$n = (int) $input;

for ($i = 1; $i <= $n; $i++) {
    for ($j = $n; $j >= 1; $j--) {
        if ($i == $j) echo $j;
        else echo " ";
    }
    for ($k = 2; $k <= $n; $k++) {
        if ($i == $k) echo $k;
        else echo " ";
    }
    echo PHP_EOL;
}

for ($i = $n - 1; $i >= 1; $i--) {
    for ($j = $n; $j >= 1; $j--) {
        if ($i == $j) echo $j;
        else echo " ";
    }
    for ($k = 2; $k <= $n; $k++) {
        if ($i == $k) echo $k;
        else echo " ";
    }
    echo PHP_EOL;
}
?>

How It Works

1. Validate input. is_numeric rejects letters; require a positive whole number.

2. Same diamond. Top and bottom halves match Example 1 — only the source of $n changes.

3. Scale height. Total rows become 2*$n − 1 for any half-height.

Example 3 — printRow Helper

Extract edge printing into printRow so top and bottom halves share one function.

PHP
<?php
$n = 5;

for ($i = 1; $i <= $n; $i++) {
    printRow($i, $n);
}
for ($i = $n - 1; $i >= 1; $i--) {
    printRow($i, $n);
}

function printRow($i, $n) {
    $row = "";
    for ($j = $n; $j >= 1; $j--) {
        $row .= ($i == $j) ? $j : " ";
    }
    for ($k = 2; $k <= $n; $k++) {
        $row .= ($i == $k) ? $k : " ";
    }
    echo $row . PHP_EOL;
}
?>

How It Works

1. Shared row. printRow builds left and right edges once for any half.

2. Two call sites. Top loop and bottom loop only choose which $i values to print.

3. Easier edits. Change edge logic in one place — both halves stay in sync.

Edge Cases & Pitfalls

Check these before calling the solution done.

mirror from n

Starting the bottom loop at $n

That reprints the widest row. Use for ($i = $n - 1; $i >= 1; $i--).

k from 1

Starting the right loop at $k = 1

That duplicates the center column. Keep $k = 2 like Program 57.

PHP_EOL inside

echo PHP_EOL inside an edge loop

That splits the hollow row across lines. Call it only after both edge loops finish.

n = 1

Single digit

Output is one line: 1 — the bottom loop never runs.

n ≤ 0

Empty output

Both outer loops skip. Validate and prompt again for clearer UX.

multi-digit

$n > 9

echo 10 prints two characters and breaks column alignment. Labs usually stick to 1–9.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
printRow helper (Example 3)O(n²)O(1) beyond the row string

There are 2n − 1 rows, and each row scans 2n − 1 positions, so total work is still O(n²).

Key Takeaways

  • Rule: hollow pyramid up (1..$n), then mirror down ($n − 1..1).
  • Skip middle twice: bottom half starts at $n − 1, not $n.
  • echo vs PHP_EOL: digits and spaces stay on the line; echo PHP_EOL advances after both edges.
  • Next step: Program 59 prints a hollow square border of numbers.

One line: print Program 57 up, then mirror from $n − 1 for a hollow diamond.

Frequently Asked Questions

Program 57 prints only the top half (hollow pyramid). Program 58 adds a mirrored bottom half to form a complete diamond.
Starting at $n would duplicate the widest middle row. $n-1 mirrors back without repeating row $n.
For size $n, the diamond has 2*$n-1 rows — $n rows up, then $n-1 rows down.
At $i=1, left and right edge positions overlap — only one digit prints.
Yes. Use variable $n in both outer loops and all inner bounds.
Second outer loop: for ($i = $n - 1; $i >= 1; $i--) — never start at $n again.
echo stays on the same line for each digit or space. echo PHP_EOL ends the row after both edge loops finish.
O(n²) because you print 2*$n-1 rows and each row scans about 2*$n-1 positions.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.

Did you know?

Program 57’s hollow pyramid printed once, then mirrored from $n-1 down to 1 — that gives a hollow diamond with 2*$n-1 rows.

Next: Hollow Square Border

Continue with a square that prints numbers only on the border.

Program 59 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful