Hollow Square of 1s in PHP

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a hollow square border of 1s in PHP using nested loops.

The key idea is to print 1 only on the border cells and print spaces for inner cells.

⭐ Pattern Output

For $n = 5, the pattern looks like this:

Output
1 1 1 1 1
1       1
1       1
1       1
1 1 1 1 1
1

Complete PHP Program

If the cell is on the border (first/last row or column), print 1. Otherwise print spaces.

PHP
<?php
$n = 5;

for ($i = 1; $i <= $n; $i++) {
    for ($j = 1; $j <= $n; $j++) {
        if ($i === 1 || $i === $n || $j === 1 || $j === $n) {
            echo "1 ";
        } else {
            echo "  ";
        }
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Choose the grid size

$n = 5; prints a 5×5 grid.

Setup
2

Loop over rows and columns

Two nested loops visit every cell ($i, $j) in the grid.

Traversal
3

Border check

If $i or $j is at the boundary (1 or $n), print 1.

Condition
=

Hollow square

You still check every cell, so runtime is O(n²).

2

Variation — User Input (CLI) Version

Let the user decide the size $n at runtime using standard input (run from terminal with php):

PHP
<?php
echo "Enter n: ";
$n = (int) trim(fgets(STDIN));

if ($n < 2) {
    exit("n must be at least 2." . PHP_EOL);
}

for ($i = 1; $i <= $n; $i++) {
    for ($j = 1; $j <= $n; $j++) {
        if ($i === 1 || $i === $n || $j === 1 || $j === $n) {
            echo "1 ";
        } else {
            echo "  ";
        }
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Print a border of another digit (like 9) instead of 1
  • Fill the inside with another symbol (like 0) to make a framed square
  • Use fixed-width formatting for consistent columns
  • Print a hollow rectangle by using different row/column sizes
  • Create a double border by checking for $i===2 or $i===$n-1

Avoid

  • Printing border and interior using the same branch (you’ll lose the hollow effect)
  • Forgetting input validation in the CLI version
  • Using <br> in CLI output (use PHP_EOL)
  • Assuming spaces render the same in HTML as in console

Key Takeaways

1

Border cells satisfy $i===1 or $i===$n or $j===1 or $j===$n.

2

Interior cells print spaces, creating the hollow shape.

3

Two nested loops visit all cells, giving O(n²).

4

The same technique works for rectangles and other borders.

❓ Frequently Asked Questions

Only border cells print 1. Interior cells print spaces, which makes the square hollow.
Yes, but use CSS grid/flex for spacing (HTML collapses spaces). Alternatively, output &nbsp; for visible spaces.
A hollow border doesn’t make sense for n=1, so the CLI variation requires n >= 2.
O(n²) because you evaluate every cell in an n×n grid.

Explore More PHP Number Patterns!

Try more hollow and border-style patterns to sharpen your conditional logic.

All Number Patterns →
Did you know?

Many hollow patterns are built with the same border check: first/last row or first/last column.

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