PHP Hollow Number Pyramid Pattern

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

What Is This Pattern?

A hollow number pyramid prints each row number only at the left and right edges — spaces fill everything between. Row 1 is a single 1; wider rows look like 3   3.

Remember
Rule: for i = 1..n
        left  j = n..1: print j if i==j else space
        right k = 2..n: print k if i==k else space

    1    
   2 2   
  3   3  
 4     4 
5       5     ← n = 5

Unlike Program 56 (filled palindrome), here only the edges show digits — the middle stays hollow. Program 58 mirrors this into a hollow diamond.

How to Solve It

For each row, scan left ($n..1) then right (2..$n). Print the index when it matches the row; otherwise print a space. End with echo PHP_EOL.

MethodIdeaBest for
Fixed nHard-code size; left + right edge loopsLabs and demos
CLI inputSame logic; read $n at runtimeInteractive practice
Compact rowsBuild a $row string, then echo onceFewer echo calls

Pseudocode

Pseudocode
for i from 1 to n:
    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;
Left edgefor ($j = $n; $j >= 1; $j--) echo $i == $j ? $j : " ";
Right edgefor ($k = 2; $k <= $n; $k++) echo $i == $k ? $k : " ";
End the rowecho PHP_EOL;
Row width2 * $n - 1 columns

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 height and the hollow pyramid 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 · width = 9
    1    
   2 2   
  3   3  
 4     4 
5       5

Worked Walkthrough

Trace $n = 5, row $i = 3 — left edge then right edge build 3   3.

PartLoopResult
Left$j = 5..1; print at $j = 33
Right$k = 2..5; print at $k = 33

Combined: 3 3 (9 columns). Middles are spaces — that is the hollow look.

PHP Programs

Three complete programs: fixed $n = 5, CLI input, and compact $row assembly. Use View Output for sample results.

Example 1 — Fixed $n = 5

Hard-coded size — left loop $j = $n..1, right loop $k = 2..$n.

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;
}
?>

How It Works

1. Left edge. Scan $j from $n down to 1; echo $j only when $i == $j.

2. Right edge. Scan $k from 2 to $n; echo $k only when $i == $k.

3. Hollow middle. Every other column is a space. Row 1 overlaps so only one 1 appears.

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;
}
?>

How It Works

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

2. Same edges. Left and right loops match Example 1 — only the source of $n changes.

3. Scale width. Each row spans 2*$n − 1 columns for any height.

Example 3 — Compact Rows

Same edge logic; build one $row string, then echo once per line.

PHP
<?php
$n = 5;

for ($i = 1; $i <= $n; $i++) {
    $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. Same shape. Edge conditions $i == $j / $i == $k are unchanged.

2. One buffer. Append digits and spaces to $row instead of echoing each character.

3. One print. echo $row . PHP_EOL writes the finished hollow line.

Edge Cases & Pitfalls

Check these before calling the solution done.

k from 1

Starting the right loop at $k = 1

That reprints the center column on every row. Keep $k = 2 so the overlap is skipped.

wrong compare

Comparing $i == $n instead of $i == $j

Only the bottom row would print digits. Match the loop variable ($j / $k) to the row.

PHP_EOL inside

echo PHP_EOL inside an edge loop

That splits the hollow row across lines. Call echo PHP_EOL only after both loops finish.

n = 1

Single digit

Output is one line: 1 — right edge overlaps left on the single column.

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 column alignment. Labs usually stick to 1–9.

Time and Space Complexity

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

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

Key Takeaways

  • Rule: left $n..1 then right 2..$n — print only when the index matches the row.
  • Start right at 2: avoids duplicating the center column that row 1 already printed.
  • echo vs PHP_EOL: digits and spaces stay on the line; echo PHP_EOL advances after both edges.
  • Next step: Program 58 mirrors these edges into a hollow number diamond.

One line: print the row number at both edges, spaces everywhere else.

Frequently Asked Questions

Row 2 places 2 at the left edge (when $i==$j) and again at the right edge (when $i==$k), with spaces between.
On row 1, the left and right edge conditions overlap at the same column — only one 1 appears.
Left loop $j=$n..1 prints $j when $i==$j (right-aligned). Right loop $k=2..$n prints $k when $i==$k (expanding outward).
Starting at 1 would duplicate the center column on every row. $k=2 skips that overlap.
Yes. Use a variable $n in all loop bounds — the pyramid scales to $n rows.
After rows 1..$n, mirror rows $n-1..1 with the same edge logic — see Program 58.
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 each of n rows scans about 2*n-1 positions.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.

Did you know?

Each row prints the row number at the left and right edges only — spaces fill the middle. Row 1 overlaps at one column; row 5 reads 5 5.

Next: Hollow Number Diamond

Continue with a diamond that prints only the edges.

Program 58 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