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.
Approach
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.
Method
Idea
Best for
Outer $i += 2
Odd lengths; inner prints 1..$i
Learning, interviews, exams
Odd digits
Inner also uses $j += 2
When 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
Goal
Pattern
Walk odd lengths
for ($i = 1; $i <= $max; $i += 2)
Print 1..$i
for ($j = 1; $j <= $i; $j++) echo $j;
End the row
echo PHP_EOL;
Even max fix
if ($max % 2 == 0) $max -= 1;
Odd digits only
for ($j = 1; $j <= $i; $j += 2) echo $j;
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j
Stays on the same line
Each digit in the run
echo PHP_EOL
Ends the current line
After the inner loop
Print all digits without a newline, then end the row once.
Try it
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 resultmax = 9 · 5 rows · 25 digits
1
123
12345
1234567
123456789
Trace
Worked Walkthrough
Trace $max = 7 — each odd outer value, the inner range, and the printed row.
$i
Inner $j
Length
Printed row
1
1..1
1
1
3
1..3
3
123
5
1..5
5
12345
7
1..7
7
1234567
Total digits: 1 + 3 + 5 + 7 = 16. For odd max n, the sum is ((n + 1) / 2)² — still O(n²).
Code
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.
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.