Square Number Pattern in PHP

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

What You’ll Learn

How to print Number Pattern 41 in PHP: consecutive square numbers arranged into rows of odd length (1, 3, 5, 7, 9).

You’ll use a counter $m and print $m * $m in each position before incrementing $m.

⭐ Pattern Output

For 5 rows (odd lengths up to 9), the pattern looks like this:

Output
1
4 9 16
25 36 49 64 81
100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625
1

Complete PHP Program

The outer loop uses odd counts (1, 3, 5, 7, 9). Each row prints that many square numbers.

PHP
<?php
$m = 1;

for ($i = 1; $i <= 9; $i += 2) {
    for ($k = 1; $k <= $i; $k++) {
        echo ($m * $m) . " ";
        $m++;
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Initialize the counter

$m = 1; is the base number whose square we print.

Setup
2

Outer loop (odd row sizes)

for ($i = 1; $i <= 9; $i += 2) creates row sizes of 1, 3, 5, 7, 9.

Row control
3

Inner loop (print squares)

The inner loop prints exactly $i values using $m * $m, then increments $m.

Square values
=

Odd-length square rows

The total printed count here is 1+3+5+7+9 = 25 values, ending at 25² = 625.

2

Variation — User Input (CLI) Version

Let the user decide the maximum odd row length at runtime (run from terminal with php):

PHP
<?php
echo "Enter the maximum odd row length (e.g., 9): ";
$maxOdd = (int) trim(fgets(STDIN));

if ($maxOdd < 1 || $maxOdd % 2 === 0) {
    exit("Please enter a positive odd number." . PHP_EOL);
}

$m = 1;
for ($i = 1; $i <= $maxOdd; $i += 2) {
    for ($k = 1; $k <= $i; $k++) {
        echo ($m * $m) . " ";
        $m++;
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Use str_pad() to align columns when values become wide
  • Print cubes using $m * $m * $m
  • Center-align the rows by printing leading spaces before each row
  • Reset $m each row to square the same values per row
  • Store values in an array for later formatting or exporting

Avoid

  • Using an even maximum in the odd-length loop (validate input)
  • Forgetting to increment $m
  • Assuming output alignment without fixed-width formatting
  • Mixing HTML spacing with CLI output

Key Takeaways

1

Values printed are squares: 1, 4, 9, 16, ...

2

Row lengths grow by 2 (odd counts): 1, 3, 5, 7, 9

3

A counter $m makes squares progress continuously across rows.

4

Runtime grows quadratically as the number of printed values increases.

❓ Frequently Asked Questions

Because the pattern prints 25 values total (1+3+5+7+9 = 25). The 25th square is 25² = 625.
Yes. Increment $m by 2 (start from 2) so you square even numbers only: 4, 16, 36, ...
Replace PHP_EOL with <br> and use CSS (grid/flex) for consistent alignment.
O(n²) as printed values grow roughly with the square of the row count.

Explore More PHP Number Patterns!

Square-number patterns are a fun way to practice arithmetic with nested loops.

All Number Patterns →
Did you know?

The sum of the first n odd numbers is . That’s why odd-length rows often appear in square-related patterns.

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