Left Edge Loop
$j = $n..1
Scan $j from $n down to 1; echo $j when $i == $j, else a space.

The hollow number pyramid prints each row number at the left and right edges only — spaces fill the middle. For $n = 5: 1, 2 2, 3 3, and so on. Each row uses a left loop $j = $n..1 and a right loop $k = 2..$n with $i == $j / $i == $k conditions. This tutorial covers edge-printing logic, live preview, worked PHP examples, edge cases, and O(n²) complexity.
$j = $n..1
Scan $j from $n down to 1; echo $j when $i == $j, else a space.
$k = 2..$n
Scan $k from 2 to $n; echo $k when $i == $k, else a space.
spaces only
Every position that is not an edge prints a space — that creates the hollow look.
2n-1 cols
Row i spans 2n-1 columns — row number at left and right edges (row 1 overlaps).
3–12 rows
Pick row count and draw the hollow pyramid instantly in the browser.
Complexity
Each row scans O(n) positions — total work grows as n².
A hollow number pyramid prints the row number at both pyramid edges and spaces everywhere else. Row 2 reads 2 2; row 3 reads 3 3 when aligned.
In PHP: outer for ($i = 1; $i <= $n; $i++), left loop $j = $n..1 with $i == $j, right loop $k = 2..$n with $i == $k, then echo PHP_EOL.
Given $n = 5, print five hollow rows — widest row has 5 at both ends.
// $n = 5 (conceptual output)
// 1
// 2 2
// 3 3
// 4 4
// 5 5 | Item | Type | Description |
|---|---|---|
$n | int | Pyramid height — number of rows (typically ≥ 1). |
$i, $j, $k | int | Row $i; left index $j; right index $k. |
| Printed output | text | 2*$n-1 columns per row; row number at left and right edges only. |
for $i from 1 to $n:
for $j from $n down to 1: echo $j if $i==$j else space
for $k from 2 to $n: echo $k if $i==$k else space
echo PHP_EOL | Approach | Idea | Best for |
|---|---|---|
| Two-loop row | Left edge loop + right edge loop | Hollow edge-only rows |
| $row string | Build row without trailing spaces | Cleaner console output — Example 3 |
| fgets(STDIN) input | (int) $input for n | User-chosen pyramid height |
| Compact output | Build a $row string then echo $row | No trailing space per row — Example 3 |
| Goal | Pattern |
|---|---|
| Set n | $n = 5; |
| Outer loop | for ($i = 1; $i <= $n; $i++) |
| Left loop | for ($j = $n; $j >= 1; $j--) echo $j if $i==$j else space |
| Right loop | for ($k = 2; $k <= $n; $k++) echo $k if $i==$k else space |
| Row break | echo PHP_EOL; after both edge loops |
| Program 56 contrast | Palindromic pyramid fills every row; this pattern prints edges only with spaces inside |
How left edge loop, right edge loop, and row breaks work together.
for ($j = $n; $j >= 1; $j--)
echo $i==$j ? $j : " "Right-aligns the left pyramid edge.
for ($k = 2; $k <= $n; $k++)
echo $i==$k ? $k : " "Expands the right pyramid edge outward.
trace $i=3, $n=5Dry-run row 3: left prints 3, right prints 3, middle is spaces.
Reach for this pattern when teaching edge conditions, hollow shapes, and nested loops.
Classic follow-up after palindromic pyramids — introduces hollow edge-printing.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible pattern size.
Compare with Program 56 (palindromic pyramid), then continue to Program 58 hollow diamond.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one program that locks in edge conditions, hollow rows, and O(n²) thinking.
Choose pattern size n and draw the full hollow number pyramid pattern in the browser.
Three complete PHP programs — fixed $n = 5, fgets(STDIN) input, and a compact $row string variant. Click View Output to reveal sample console results.
Print five hollow rows — widest row shows 5 at both edges.
$n = 5Hard-coded $n = 5 — left loop $j = $n..1, right loop $k = 2..$n, edge conditions $i==$j / $i==$k.
<?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;
} Row 1 prints one 1 where edges overlap. Row 3 prints 3 at the left edge and 3 at the right — spaces fill the middle.
Read $n with fgets(STDIN) for flexible output size.
Same hollow edge logic; pyramid height comes from user input.
<?php
echo "Enter n: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . 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;
} Same hollow edge logic as Example 1; height comes from fgets(STDIN) input.
Build each row with string concatenation — same edge logic, cleaner assembly.
Same edge logic; string concatenation assembles left and right halves in one buffer.
<?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;
} Same edge logic; building a $row string avoids repeated echo calls per character.
Set $n (e.g. 5). Outer loop for ($i = 1; $i <= $n; $i++) builds one hollow row per iteration.
for ($j = $n; $j >= 1; $j--) echoes $j when $i==$j, else a space.
for ($k = 2; $k <= $n; $k++) echoes $k when $i==$k, else a space.
echo PHP_EOL after both edge loops finish — starts the next row.
Each row scans 2n-1 positions (e.g. 9 columns when n=5) — O(n²) time, O(1) extra memory.
$i = 3, $n = 5Trace row 3 to see left and right edge loops form 3 3.
| Phase | Loop | Row so far |
|---|---|---|
| Left edge | $j=5..1, $i==3 at $j=3 | 3 |
| Right edge | $k=2..5, $i==3 at $k=3 | 3 3 |
Final row 3: 3 3. Then echo PHP_EOL moves to row 4.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: use fgets(STDIN) for dynamic rows — see Example 2.
Split each row into left half ($j=$n..1) and right half ($k=2..$n) for edge control.
Example: trace row 3 with n=5 — left prints 3 at column 3, right at column 5.
Practice echo PHP_EOL vs echo for multi-line vs same-line output.
Example: use string concatenation for clean rows — see Example 3.
Only edge positions print digits — interior stays empty for the hollow effect.
Example: row 4 reads 4 4 — two fours at the edges.
Each row scans O(n) positions — total work grows as n².
Example: count columns on row 5 → 2×5-1 = 9 character positions per row.
Pair the pattern with fgets(STDIN) and positive-row checks.
Example: reject $n <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner loop 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.
Missing right edge loop shows immediately — only left digits appear.
Only loops and console output — no arrays or math libraries.
Change $n, use fgets(STDIN), mirror into a diamond (Program 58), or build rows with a $row string.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the two-loop row (left edge, right edge) first; then try fgets(STDIN) input and the $row string variant in Example 3.
Small habits that keep number-pattern code clean.
Use n for height and i/j/k for loop variables.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Finish the inner loops for row $i, then call echo PHP_EOL.
Starting at k = 1 duplicates the center on row 1 — use k = 2 to skip overlap.
Trace $n = 5, $i = 3 on paper — expect 3 3 with spaces between edges.
Pro Tip: if row 1 shows two digits, check whether the right loop starts at k = 2.
Mistakes that commonly break hollow number pyramid patterns.
Each number lands on its own line — you get a vertical stack, not a centered pyramid row.
→ Use echo inside left and right edge loops; echo PHP_EOL only after they finish.
Without the k = 2..n loop, only the left edge prints — no right-side digit.
→ Add the right loop: for ($k = 2; $k <= $n; $k++) with $i == $k condition.
Omitting echo PHP_EOL after both edge loops glues all rows onto one line.
→ Always call echo PHP_EOL after left and right edge loops complete.
Starting $k = 1 duplicates the center on row 1 — use $k = 2 instead.
→ Start the right half at $k = 2 to skip the overlapping center column.
Letters or empty input throw invalid input without validation.
→ Use is_numeric($input) before (int) $input.
Using literal 5 in loop bounds instead of variable $n breaks dynamic input.
→ Use one $n variable for outer loop and both inner loop bounds.
Check these inputs before calling the solution done.
Output is one line: 1 — right edge overlaps left on the single column.
Loop never runs — print nothing or show a message.
$n < 0Treat as invalid; re-prompt instead of silent empty output.
Large values produce wide rows — fine for labs; use smaller n for quick demos.
Unchecked fgets(STDIN) input leaves $n unset — call is_numeric($input) first.
Use conditional spacing to avoid trailing spaces — see Example 3.
Try these variations to lock in the pattern.
$n = 3, 6, or 8i shows i at both edges$row string output2n-1 columns; only two (or one on row 1) print digits.echo stays on the line; echo PHP_EOL advances — mix them carefully.$n > 0 for interactive programs; $n = 1 prints one digit where edges overlap.$j=$n..1, then right loop $k=2..$n, edge conditions only.Quick Takeaway: outer $i=1..$n; left loop $j=$n..1; right loop $k=2..$n; then echo PHP_EOL.
| Program | Time | Extra space |
|---|---|---|
Fixed $n = 5 (Example 1) | O(n²) | O(1) |
| fgets(STDIN) input (Example 2) | O(n²) | O(1) |
| Compact rows (Example 3) | O(n²) | O(1) |
The hollow number pyramid combines left/right edge loops with space padding — a natural step after the palindromic pyramid in Program 56. Master the fixed-$n version first, then try fgets(STDIN) input and the compact $row string variant in Example 3.
Practice the three examples above, then continue to Program 58 for the hollow number diamond pattern.
Print edge digits only — spaces everywhere else — one echo PHP_EOL per outer iteration.
i, left loop, and right loop before codingecho PHP_EOL$n ≥ 1 for interactive programsfgets(STDIN) return value before using $nk = 1 (duplicates center on row 1)$n$n = 1 edge casePrint row number at left and right edges; spaces fill the hollow middle.
Row $i has 2 edge digits (1 when $i=1)
$i = 1..$n
CodeLeft $j=$n..1, right $k=2..$n
Logic2n-1 cols/row
O(n²)
AnalysisEach row prints the row number at the left and right edges only — everything between is spaces. Row 1 overlaps at one position; row 5 reads 5 5.
Move on to the hollow number diamond pattern in the PHP number-pattern series.
12 people found this page helpful