Odd/Even Row Number Pattern in PHP

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Conditional Logic

What You’ll Learn

How to print a row-wise odd/even number pattern in PHP. Odd rows print odd numbers (1 3 5 ...) and even rows print even numbers (2 4 6 ...), with the count of numbers increasing each row.

You’ll use an if condition to pick the starting value and a nested loop to print values by adding 2 each time.

⭐ Pattern Output

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

Output
1
2 4
1 3 5
2 4 6 8
1 3 5 7 9
1

Complete PHP Program

We start each row with 1 (odd row) or 2 (even row), then keep adding 2 to stay in the same parity.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    $k = ($i % 2 === 0) ? 2 : 1;
    for ($j = 1; $j <= $i; $j++) {
        echo $k . " ";
        $k += 2;
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Pick the number of rows

$rows = 5; sets how many lines to print.

Setup
2

Outer loop (rows)

for ($i = 1; $i <= $rows; $i++) controls how many numbers appear on each row ($i numbers on row $i).

Row control
3

Choose odd/even start

$k = ($i % 2 === 0) ? 2 : 1; sets $k to 2 for even rows and 1 for odd rows.

Parity
4

Inner loop (print +2 sequence)

The inner loop prints $k and then does $k += 2, so the row stays odd or even.

Printing
=

Odd/even row triangle

Odd rows print odd sequences, even rows print even sequences—and each row grows by one number.

2

Variation — User Input (CLI) Version

Let the user decide the number of rows at runtime using standard input (run from terminal with php):

PHP
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));

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

for ($i = 1; $i <= $rows; $i++) {
    $k = ($i % 2 === 0) ? 2 : 1;
    for ($j = 1; $j <= $i; $j++) {
        echo $k . " ";
        $k += 2;
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Print without trailing spaces by building an array and joining with spaces
  • Start even rows from 0 instead of 2 for a 0-based variant
  • Right-align the triangle by printing leading spaces before each row
  • Replace numbers with symbols to create alternating-row symbol patterns
  • Increase $rows to generate a bigger triangle

Avoid

  • Forgetting the newline after each row
  • Resetting $k incorrectly (it must restart for each row)
  • Mixing row/column logic (outer loop = row size)
  • Skipping input validation in the CLI version

Key Takeaways

1

The outer loop sets how many numbers to print on each row.

2

Row parity decides whether we start at 1 (odd) or 2 (even).

3

Adding 2 keeps the sequence odd-only or even-only.

4

This technique generalizes to other step sizes and alternating rules.

❓ Frequently Asked Questions

It checks $i % 2. If the row number is even, it starts at 2; otherwise it starts at 1.
Yes—change echo $k . " "; to echo $k;. For large values, spaces are usually more readable.
Set the even-row start to 0 instead of 2, and keep adding 2.
O(n²) for n rows, because total prints are \(1+2+\dots+n\).

Explore More PHP Number Patterns!

Try mixing conditional logic with step-size loops to build rich custom patterns.

All Number Patterns →
Did you know?

You can generate arithmetic sequences quickly by adding a constant each step. Here we add 2 to stay on odds or evens, but the same idea works for +3, +5, and more.

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