Odd/Even Row Number Pattern in PHP

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:
1
2 4
1 3 5
2 4 6 8
1 3 5 7 9Complete 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
$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
Pick the number of rows
$rows = 5; sets how many lines to print.
Outer loop (rows)
for ($i = 1; $i <= $rows; $i++) controls how many numbers appear on each row ($i numbers on row $i).
Choose odd/even start
$k = ($i % 2 === 0) ? 2 : 1; sets $k to 2 for even rows and 1 for odd rows.
Inner loop (print +2 sequence)
The inner loop prints $k and then does $k += 2, so the row stays odd or even.
Odd/even row triangle
Odd rows print odd sequences, even rows print even sequences—and each row grows by one number.
Variation — User Input (CLI) Version
Let the user decide the number of rows at runtime using standard input (run from terminal with 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
0instead of2for 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
$rowsto generate a bigger triangle
Avoid
- Forgetting the newline after each row
- Resetting
$kincorrectly (it must restart for each row) - Mixing row/column logic (outer loop = row size)
- Skipping input validation in the CLI version
Key Takeaways
The outer loop sets how many numbers to print on each row.
Row parity decides whether we start at 1 (odd) or 2 (even).
Adding 2 keeps the sequence odd-only or even-only.
This technique generalizes to other step sizes and alternating rules.
❓ Frequently Asked Questions
$i % 2. If the row number is even, it starts at 2; otherwise it starts at 1.echo $k . " "; to echo $k;. For large values, spaces are usually more readable.0 instead of 2, and keep adding 2.Explore More PHP Number Patterns!
Try mixing conditional logic with step-size loops to build rich custom patterns.
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.
12 people found this page helpful
