PHP Hollow Square Pattern (Border of 1s)

Definition
What Is This Pattern?
A hollow square border prints 1 only on the edges of an n × n grid and leaves the interior as spaces.
Rule: print 1 on the border; spaces inside
1 1 1 1 1
1 1
1 1
1 1
1 1 1 1 1 ← size = 5
In PHP loop $i and $j from 1 to $size. Print "1 " when the cell is on the border; otherwise print " ".
Approach
How to Solve It
One nested loop over every cell, plus a four-way border check.
| Method | Idea | Best for |
|---|
| Border condition | If $i/$j on edge → 1, else spaces | Learning, interviews, exams |
| Size input | Same logic with a user-chosen $size | Practice / demos |
Pseudocode
for i from 1 to size:
for j from 1 to size:
if i is 1 or size, or j is 1 or size:
print "1 "
else:
print " "
print newline
Cheat sheet
| Goal | Pattern |
|---|
| Walk rows | for ($i = 1; $i <= $size; $i++) |
| Walk columns | for ($j = 1; $j <= $size; $j++) |
| Border test | if ($i == 1 || $i == $size || $j == 1 || $j == $size) |
| Print border / interior | echo "1 "; / echo " "; |
| End the row | echo PHP_EOL; |
Printing Numbers vs Starting a New Line
| API | Effect | Use for |
|---|
echo "1 " / echo " " | Stays on the same line | Each cell |
echo PHP_EOL | Ends the current line | After the inner loop finishes a row |
Print cells without a newline, then end the row once.
Try it
Live Preview
Change the square size and the hollow border updates instantly.
Trace
Worked Walkthrough — $size = 4
Trace each row: top and bottom are solid; middle rows keep only the ends.
$i | Border rule | Printed row |
|---|
1 | Top row — all 1 | 1 1 1 1 |
2 | $j == 1 or $j == 4 | 1 1 |
3 | $j == 1 or $j == 4 | 1 1 |
4 | Bottom row — all 1 | 1 1 1 1 |
Border cells for size n = 4(n - 1) when n >= 2 (corners counted once).
Code
PHP Programs
Three complete programs: fixed 5×5, CLI input size, and an asterisk-border variant. Use View Output for sample results.
Example 1 — Fixed $size = 5
Nested loops with a border condition — ideal for first demos.
<?php
$size = 5;
for ($i = 1; $i <= $size; $i++) {
for ($j = 1; $j <= $size; $j++) {
if ($i == 1 || $i == $size || $j == 1 || $j == $size)
echo "1 ";
else
echo " ";
}
echo PHP_EOL;
}
1 1 1 1 1
1 1
1 1
1 1
1 1 1 1 1
How It Works
1. Grid walk. Outer $i picks the row; inner $j picks the column — every cell is visited once.
2. Border test. If $i or $j is on the edge, print "1 "; otherwise print " ".
3. New line. echo PHP_EOL after the inner loop starts the next row.
Example 2 — User Input (size)
Read the square size from STDIN and build an n × n hollow frame.
<?php
echo "Enter square size: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 2) {
echo "Please enter an integer greater than 1." . PHP_EOL;
exit(1);
}
$size = (int) $input;
for ($i = 1; $i <= $size; $i++) {
for ($j = 1; $j <= $size; $j++) {
if ($i == 1 || $i == $size || $j == 1 || $j == $size)
echo "1 ";
else
echo " ";
}
echo PHP_EOL;
}
How It Works
1. Validate size. Check is_numeric; require $size >= 2 for a hollow frame.
2. Same border logic. Only the bound changes from the literal 5 to the user value.
Example 3 — Asterisk Border ($size = 5)
Same hollow frame — swap 1 for * on the border.
<?php
$size = 5;
for ($i = 1; $i <= $size; $i++) {
for ($j = 1; $j <= $size; $j++) {
if ($i == 1 || $i == $size || $j == 1 || $j == $size)
echo "* ";
else
echo " ";
}
echo PHP_EOL;
}
* * * * *
* *
* *
* *
* * * * *
How It Works
1. Same structure. Only the border string changes from "1 " to "* ".
2. Keep spacing. Interior cells still use two spaces so columns stay aligned.
Edge Cases & Pitfalls
Check these before calling the solution done.
AND vs ORUse && instead of ||
Corners would still print, but side edges would be empty. Border needs || so any edge matches.
one spaceInterior prints a single space
Columns drift because border cells use "1 " (two characters). Match width with " ".
size = 1Single cell
Output is just 1 — no hollow interior. Require $size >= 2 for a frame.
0-basedLoops from 0 but check $size
If indices are 0..$size-1, test $i == 0 || $i == $size - 1 (and the same for $j).
PHP_EOL earlyecho PHP_EOL inside the inner loop
That puts every cell on its own line. End the row only after the column loop.
Bad input(int) alone is not enough
Prefer is_numeric after trim(fgets(STDIN)) so non-numeric input does not silently become 0.
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| Fixed / asterisk (Examples 1, 3) | O(n²) | O(1) |
| User input (Example 2) | O(n²) | O(1) |
Every cell of the n × n grid is visited once, so work is quadratic in the side length.
Remember
Key Takeaways
Rule: print 1 when $i or $j is on the edge; otherwise print spaces.
Align: use "1 " and " " so every cell is two characters wide.
echo vs PHP_EOL: cells stay on the line; echo PHP_EOL advances after each row.
Next step: Program 43 prints a right-aligned ascending number triangle.
One line: visit every cell; print 1 on the border and spaces inside.
Frequently Asked Questions
It prints characters only on the border (first/last row and first/last column) and leaves the inside blank with spaces.
It checks if $i is 1 or $size, or if $j is 1 or $size. If any is true, print 1; otherwise print spaces.
Use a $size variable and loop from 1 to $size for both $i and $j. Update the border check to use $size — see Example 2.
Each border cell uses "1 " (digit plus space). Interior cells use " " so columns stay aligned.
Size 2 gives a thin frame. Size 1 prints a single 1 with no hollow interior.
Program 41 prints a centered pyramid of perfect squares. Program 42 prints a hollow square grid using border conditions.
Replace "1 " with "* " or "# " in the if branch — see Example 3.
O(n²) for an n×n grid because each cell is visited once.
Use trim(fgets(STDIN)) and check is_numeric($input); validate $size >= 2 for a meaningful hollow frame.
🤔
Did you know?
Print 1 when $i == 1, $i == $size, $j == 1, or $j == $size; otherwise print spaces. An n × n grid visits n² cells.
Next: Right-Aligned Number Triangle
Print 1, 1 2, 1 2 3… with leading spaces for right alignment.
Program 43 tutorial →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.
6 people found this page helpful