Increasing Odd-Length Number Pattern in PHP

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Step Size Loops

What You’ll Learn

How to print an increasing odd-length number pattern in PHP:

1, 123, 12345, 1234567, 123456789.

The trick is to increase the row limit by 2 each time, so the number of digits per row stays odd.

⭐ Pattern Output

For a maximum width of 9, the pattern looks like this:

Output
1
123
12345
1234567
123456789
1

Complete PHP Program

The outer loop picks the row width (1, 3, 5, 7, 9). The inner loop prints numbers from 1 to that width.

PHP
<?php
$max = 9;

for ($i = 1; $i <= $max; $i += 2) {
    for ($j = 1; $j <= $i; $j++) {
        echo $j;
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Set the maximum width

$max = 9; sets the last row length (odd).

Setup
2

Outer loop (odd row lengths)

for ($i = 1; $i <= $max; $i += 2) produces widths 1, 3, 5, 7, 9.

Row control
3

Inner loop (print 1..i)

for ($j = 1; $j <= $i; $j++) prints digits from 1 to the current row width.

Printing
4

New line

echo PHP_EOL; moves to the next row.

Line break
=

Odd-length growth

The row length grows by 2 each time, so it stays odd and reaches 9 on the last row.

2

Variation — User Input (CLI) Version

Let the user choose the maximum odd width (run from terminal with php):

PHP
<?php
echo "Enter the maximum width (odd number): ";
$max = (int) trim(fgets(STDIN));

if ($max < 1) {
    echo "Please enter a positive number." . PHP_EOL;
    exit(1);
}

if ($max % 2 === 0) {
    $max -= 1; // keep widths odd: 1,3,5,...
}

for ($i = 1; $i <= $max; $i += 2) {
    for ($j = 1; $j <= $i; $j++) {
        echo $j;
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits for readability (echo $j . " ";)
  • Print the descending variant by looping $j from $i down to 1
  • Start from 0 by printing $j - 1 or adjusting the loop
  • Right-align the pattern by printing leading spaces
  • Use a different sequence (even numbers, odds only, etc.) inside the inner loop

Avoid

  • Using an even max width without adjusting (you’ll get an extra even-length row)
  • Forgetting the newline after each row
  • Mixing up row/column logic (outer loop = row width)
  • Skipping input validation in CLI mode

Key Takeaways

1

The outer loop increases by 2 to generate odd row lengths.

2

The inner loop prints 1 through the current row limit.

3

Setting $max controls the final row width.

4

Step-size loops are a handy technique for selecting only odd or even counts.

❓ Frequently Asked Questions

Because the outer loop uses $i += 2, so row widths increase by 2 each time.
Change the outer loop to increase by 1 ($i++) so each row length grows by one instead of two.
Yes—use echo $j . " "; inside the inner loop.
O(n²) relative to the max width n. Total prints grow quadratically.

Explore More PHP Number Patterns!

Odd-length patterns are a great stepping stone to pyramids, diamonds, and centered patterns.

All Number Patterns →
Did you know?

Increasing by 2 is the simplest way to iterate over odd counts (or odd numbers). The same trick works for evens by starting at 2.

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