Shape Rule
Border only
Only first/last row and first/last column print 1; interior cells stay blank.

The hollow square of 1s prints a border of ones with blank space inside. This tutorial covers the border condition, nested loops, a live preview, algorithm steps, worked PHP examples, edge cases, and complexity.
Border only
Only first/last row and first/last column print 1; interior cells stay blank.
$j = 1..$rows
for ($j = 1; $j <= $rows; $j++) walks every column in the current row.
Four edges
if ($i==1 || $i==$rows || $j==1 || $j==$rows) prints "1 "; otherwise " ".
Row index i
for ($i = 1; $i <= $rows; $i++) walks each row of the square grid.
1–20 rows
Pick a row count and draw the hollow square of 1s instantly in the browser.
Complexity
Visits every cell in an n×n grid — n² iterations; extra memory stays O(1).
A hollow square of 1s prints a border of ones with empty space inside. With $rows = 5, the output is a 5×5 grid: full top and bottom rows of ones, and middle rows with ones only at the left and right edges.
In PHP you use nested loops over rows and columns, then an if that checks whether the current cell lies on the border. Border cells print "1 "; inner cells print " " to keep columns aligned.
It teaches boundary detection with a simple condition — a key step before hollow rectangles, frames, and diamonds.
Outer loop uses $i = 1..$rows; inner loop uses $j = 1..$rows.
First/last row or first/last column → print 1.
Border uses "1 " and interior uses " " so columns line up.
Follow Program 41 square pyramid; continue to Program 43 right-aligned triangle.
In short: for each row $i and column $j, print "1 " on the border and " " inside, then call echo PHP_EOL after each row.
Given a positive integer $rows (e.g. 5), print an n×n hollow square: border cells show 1, inner cells show spaces.
// $rows = 5 (conceptual shape)
// 1 1 1 1 1
// 1 1
// 1 1
// 1 1
// 1 1 1 1 1 | Item | Type | Description |
|---|---|---|
$rows | int | Number of rows (and columns) in the square (typically ≥ 1). |
| Printed output | text | Square grid of $rows × $rows cells; border prints 1, interior prints spaces. |
for $i from 1 to $rows:
for $j from 1 to $rows:
if border cell: print "1 "
else: print " "
print newline | Approach | Idea | Best for |
|---|---|---|
| Border condition | 1 1 1 1 1 top row, hollow middle | Learning and interviews |
| User-input size | (int) $input; | Flexible console programs |
| Filled square | Always echo "1 " | Contrast with hollow logic |
| Goal | Pattern |
|---|---|
| Walk rows | for ($i = 1; $i <= $rows; $i++) |
| Walk columns | for ($j = 1; $j <= $rows; $j++) |
| Border check | if ($i==1 || $i==$rows || $j==1 || $j==$rows) |
| End the row | echo PHP_EOL; |
| Program 41 contrast | Square pyramid uses odd widths; this pattern uses a full grid + border test |
Same square cell — how the border check decides between 1 and spaces.
$i==1 || $i==$rows
|| $j==1 || $j==$rowsTrue on any edge cell — print "1 "
else branchInterior positions print " " (two spaces)
$j = 1..$rowsEvery row has exactly $rows cells
trace $i=3Dry-run row 3: 1 at $j=1 and $j=5, spaces in between
Reach for this pattern when teaching boundary checks inside a full row×column grid.
Classic follow-up after centered pyramids and alternating rows.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 41 (square pyramid), then continue to Program 43 (right-aligned triangle).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a row count between 1 and 20 and draw the hollow square of 1s in the browser.
Three complete PHP programs — fixed row count, fgets(STDIN) input, and a filled-square variant. Click View Output to reveal sample console results.
Print five rows with two nested loops per line.
$rows = 5Hard-coded size — nested loops and a border check build each row.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $rows; $j++) {
if ($i == 1 || $i == $rows || $j == 1 || $j == $rows) {
echo "1 ";
} else {
echo " ";
}
}
echo PHP_EOL;
} When $i = 1, every $j hits the border test (top row) — five 1s print. When $i = 3, only $j = 1 and $j = 5 pass the test; the middle columns print spaces.
Let the user choose the height at runtime.
Read the row count with trim(fgets(STDIN)) (check is_numeric($input) in real apps).
<?php
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . PHP_EOL;
exit(1);
}
$rows = (int) $input;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $rows; $j++) {
if ($i == 1 || $i == $rows || $j == 1 || $j == $rows) {
echo "1 ";
} else {
echo " ";
}
}
echo PHP_EOL;
} Same nested-loop core as Example 1; only the source of $rows changes. Non-numeric input fails is_numeric($input) — validate before casting to int for safer labs.
Remove the border check to print a completely filled square.
Remove the border if and always print "1 " to get a solid square of ones.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $rows; $j++) {
echo "1 ";
}
echo PHP_EOL;
} Same nested-loop structure; the inner loop always prints "1 " with no border condition.
echo is built in; use fgets(STDIN) when reading input. Set $rows (fixed or from input).
for ($i = 1; $i <= $rows; $i++) — walks each row of the square grid.
for ($j = 1; $j <= $rows; $j++) visits every column in the current row.
Border cells print "1 "; inner cells print " ", then echo PHP_EOL ends the row.
Total cell visits: n² — O(n²) time, O(1) extra memory.
$rows = 5, row $i = 3Trace one middle row to see which cells hit the border test.
$j | Border? | Prints |
|---|---|---|
1 | yes ($j==1) | 1 |
2 | no | |
3 | no | |
4 | no | |
5 | yes ($j==$rows) | 1 |
Row output: 1 1 — total cell visits for the full square: 5×5 = 25 = n².
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change $j <= $rows to $j <= $i and watch the shape change.
Foundation for hollow rectangles, diamonds, and framed grids.
Example: add a cols variable for a hollow rectangle.
Practice echo vs row newline without complex math.
Example: put echo PHP_EOL inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: swap 1 for * once the border logic works.
Every cell is visited once — n² makes O(n²) concrete for beginners.
Example: count cell visits for n = 5 → 25.
Pair the pattern with fgets(STDIN) and positive-row checks.
Example: reject $rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner 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.
Wrong bounds show up immediately as a misaligned or filled shape.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the border-check version first; then try the filled square in Example 3.
Small habits that keep number-pattern code clean.
Use $rows (or $n) and keep $i/$j for row/column — or rename to $row/$col.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Only call echo PHP_EOL after the inner loop finishes the row.
One-liner: echo ($i==1||$i==$rows||$j==1||$j==$rows ? "1 " : " ");
Trace $rows = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of cells per line, you almost certainly put echo PHP_EOL inside the inner loop.
Mistakes that commonly break hollow squares of 1s.
Each cell lands on its own line — you get a column, not a square.
→ Use echo for each cell; echo PHP_EOL only after the inner loop.
Using $j <= $i instead of $j <= $rows produces a triangle, not a square.
→ Keep for ($j = 1; $j <= $rows; $j++) so every row has the same width.
Omitting echo PHP_EOL glues every cell onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input leave $rows unset.
→ Use is_numeric($input) and re-prompt on failure.
Printing one space instead of two breaks column alignment with border 1 cells.
→ Use " " (two spaces) for inner cells so columns stay aligned.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
$rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n² characters — fine for labs, noisy for huge n.
Unchecked fgets(STDIN) leaves $rows unset — call is_numeric($input) first.
Remove the border if and always print "1 " — see Example 3.
Try these variations to lock in the pattern.
$m * $mcols variable separate from $rows"1 " with "* "n² — border cells are 4n - 4 when n > 1.echo stays on the line; echo PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 should print a single 1."1 " on borders and " " inside so columns stay aligned.Quick Takeaway: nested row/column loops, border check for edges, fixed-width print tokens, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O($rows²) | O(1) |
| Filled square (Example 3) | O($rows²) | O(1) |
The hollow square of 1s combines nested loops with a simple border condition — a natural step after centered pyramids. Master the fixed-$rows version first, then try user input and the filled square.
Practice the three examples above, then continue to Program 43 for the right-aligned increasing number triangle.
Every row has $rows cells — keep echo PHP_EOL only after the inner loop finishes.
if before coding"1 " / " " and echo PHP_EOL after each row$rows ≥ 1 for interactive programsfgets(STDIN) return value before using $rowsecho PHP_EOL inside the inner cell loop$rows = 1 edge casePrint the pattern the beginner-friendly way.
Border cells print 1
DefinitionFour edge conditions
CodePrint two spaces
Logicn² cells
I/OO(n²) time
AnalysisOnly border cells print 1 — when $i is the first or last row, or $j is the first or last column. Inner cells print spaces, which creates the hollow look.
Move on to the right-aligned increasing number triangle in the PHP number-pattern series.
12 people found this page helpful