Home PHP Number Patterns Program 57 PHP Hollow Number Pyramid Pattern Definition
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.
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.
Approach
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.
Method Idea Best for Fixed n Hard-code size; left + right edge loops Labs and demos CLI input Same logic; read $n at runtime Interactive practice Compact rows Build a $row string, then echo once Fewer echo calls
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 Goal Pattern Set size $n = 5;Left edge for ($j = $n; $j >= 1; $j--) echo $i == $j ? $j : " ";Right edge for ($k = 2; $k <= $n; $k++) echo $i == $k ? $k : " ";End the row echo PHP_EOL;Row width 2 * $n - 1 columns
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_EOLEnds the current line After both edge loops finish
Build the full hollow row with echo, then break once.
Try it
Live Preview Change the height and the hollow pyramid updates instantly.
Trace
Worked Walkthrough Trace $n = 5, row $i = 3 — left edge then right edge build 3 3.
Part Loop Result Left $j = 5..1; print at $j = 33Right $k = 2..5; print at $k = 33
Combined: 3 3 (9 columns). Middles are spaces — that is the hollow look.
Code
PHP Programs Three complete programs: fixed $n = 5, CLI input, and compact $row assembly. Use View Output for sample results.
1. Fixed n 2. User input 3. Compact rows Example 1 — Fixed $n = 5 Hard-coded size — left loop $j = $n..1, right loop $k = 2..$n.
<?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
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
$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 loopThat 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 > 9echo 10 prints two characters and breaks column alignment. Labs usually stick to 1–9.
Analysis
Time and Space Complexity Program Time Extra 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²).
Remember
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 Why does row 2 print 2 2? Row 2 places 2 at the left edge (when $i==$j) and again at the right edge (when $i==$k), with spaces between.
Why is the first row just 1? On row 1, the left and right edge conditions overlap at the same column — only one 1 appears.
How are the left and right edges positioned? Left loop $j=$n..1 prints $j when $i==$j (right-aligned). Right loop $k=2..$n prints $k when $i==$k (expanding outward).
Why does the right loop start at k=2? Starting at 1 would duplicate the center column on every row. $k=2 skips that overlap.
Can I change 5 to any n? Yes. Use a variable $n in all loop bounds — the pyramid scales to $n rows.
How do I turn this into a full diamond? After rows 1..$n, mirror rows $n-1..1 with the same edge logic — see Program 58.
What is the difference between echo and PHP_EOL? echo stays on the same line for each digit or space. echo PHP_EOL ends the row after both edge loops finish.
What is the time complexity? O(n²) because each of n rows scans about 2*n-1 positions.
How should I read n safely? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion