PHP Number Rows Pattern (Odd Length)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An increasing odd-length number rows pattern prints consecutive digits 1..$i on each row, with row lengths growing by 2 each time (1, 3, 5, 7, 9).

Remember
Rule: for odd i from 1 to max, print j = 1..i (no spaces)

1
123
12345
1234567
123456789   ← max = 9

In PHP the outer loop uses $i += 2 so only odd lengths appear, the inner loop prints $j from 1 to $i with echo $j, then echo PHP_EOL ends the row. Program 23 continues with a number and asterisk mirror.

How to Solve It

Outer loop from 1 to $max with step 2. Inner loop prints 1..$i with no spaces. End each row with echo PHP_EOL.

MethodIdeaBest for
Outer $i += 2Odd lengths; inner prints 1..$iLearning, interviews, exams
Odd digitsInner also uses $j += 2When you want 1, 13, 135… instead of 1..i

Pseudocode

Pseudocode
for i from 1 to max step 2:
    for j from 1 to i:
        print j (no space)
    print newline

Cheat sheet

GoalPattern
Walk odd lengthsfor ($i = 1; $i <= $max; $i += 2)
Print 1..$ifor ($j = 1; $j <= $i; $j++) echo $j;
End the rowecho PHP_EOL;
Even max fixif ($max % 2 == 0) $max -= 1;
Odd digits onlyfor ($j = 1; $j <= $i; $j += 2) echo $j;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $jStays on the same lineEach digit in the run
echo PHP_EOLEnds the current lineAfter the inner loop

Print all digits without a newline, then end the row once.

Live Preview

Change the odd maximum and the pattern updates instantly — capped at 9 so every digit stays a single character.

Odd whole numbers from 1 to 9 (single digits). Even values are adjusted down by 1.

Live result max = 9 · 5 rows · 25 digits
1
123
12345
1234567
123456789

Worked Walkthrough

Trace $max = 7 — each odd outer value, the inner range, and the printed row.

$iInner $jLengthPrinted row
11..111
31..33123
51..5512345
71..771234567

Total digits: 1 + 3 + 5 + 7 = 16. For odd max n, the sum is ((n + 1) / 2)² — still O(n²).

PHP Programs

Three complete programs: fixed max, CLI input with even fix, and odd-only digits. Use View Output for sample results.

Example 1 — Fixed $max = 9

Outer loop steps by 2; inner loop concatenates digits 1..$i.

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. Odd lengths only. $i += 2 walks 1, 3, 5, 7, 9 — each value is both the row index and the row length.

2. Concatenate digits. echo $j with no space builds 123, 12345, and so on.

3. End the row. echo PHP_EOL; after the inner loop starts the next longer row.

Example 2 — User Input

Read $max from the CLI; adjust to odd if the user enters an even value.

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

if ($max % 2 == 0) {
    $max -= 1;
}

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

How It Works

1. Read max. Ask for a maximum, then trim(fgets(STDIN)) and cast to int.

2. Keep it odd. If the user types 8, subtract 1 so the last row length is still 7.

3. Safer input tip. Bare (int) casts turn letters into 0. Prefer:

Safer input
echo "Enter the maximum value: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
    echo "Enter a positive whole number." . PHP_EOL;
    exit(1);
}
$max = (int) $input;
if ($max % 2 == 0) {
    $max -= 1;
}

Example 3 — Odd-Only Digits

Keep odd row lengths, but print only odd digits with $j += 2.

PHP
<?php
$max = 9;

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

How It Works

1. Same outer loop. Row lengths are still 1, 3, 5, 7, 9.

2. Different digits. $j += 2 prints 1, 3, 5… up to $i instead of every integer.

3. Shorter rows. Length in characters is half of Example 1 (rounded up) — same odd outer step, different content.

Edge Cases & Pitfalls

Check these before calling the solution done.

i++

Using $i++ instead of $i += 2

That prints every length 1, 2, 3, 4… Keep the step at 2 for odd lengths only.

spaces

Adding spaces between digits

echo $j . " " produces 1 2 3 instead of 123. Use bare echo $j.

PHP_EOL inside

echo PHP_EOL inside the inner loop

That puts each digit on its own line. Call echo PHP_EOL only after the row finishes.

max = 1

Single digit

Output is just 1 — a good sanity check.

even max

Even maximum without a fix

Without subtracting 1, $i <= 8 still works but the last odd length is 7. Explicitly adjust for clarity.

max ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input / odd digits (Examples 1–3)O(n²)O(1)

Total digit prints for odd max n: 1 + 3 + 5 + … + n = ((n + 1) / 2)² — still quadratic in n.

Key Takeaways

  • Rule: for each odd $i up to $max, print digits 1..$i with no spaces.
  • Step by 2: $i += 2 is what creates odd row lengths.
  • echo vs PHP_EOL: digits stay on the line; echo PHP_EOL advances after each row.
  • Next step: Program 23 mirrors numbers around a growing asterisk block.

One line: for odd $i, echo $j from 1 to $i, then echo PHP_EOL.

Frequently Asked Questions

Because the outer loop uses $i += 2, so $i becomes 1, 3, 5, 7, 9 — each row prints one more odd count of digits than the previous.
echo $j prints each digit immediately after the previous one on the same line — no space character is added.
Yes. Change the inner loop to $j += 2 and print $j to output 1, 13, 135, 1357, 13579 (see Example 3).
echo $j stays on the same line. echo PHP_EOL ends the current line. Digits use echo; the row break uses echo PHP_EOL after the inner loop.
Because the outer loop condition is $i <= 9. Change 9 to any odd maximum to extend the pattern.
Subtract 1 to make it odd (see Example 2) so the last row still has an odd length.
O(n²) for maximum row length n because total prints are 1+3+5+...+n.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int so bad input does not produce unexpected results.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Each row length increases by 2 because the outer loop uses $i += 2 (1, 3, 5, 7, 9). The inner loop prints 1..$i with no spaces — total prints grow as O(n²) for maximum row length n.

Next: Number & Asterisk Mirror

Continue with numbers mirrored around a growing star block.

Program 23 tutorial →

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