PHP Descending Number Triangle Pattern (Odd Length)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An odd-length descending number triangle prints ascending digits 1..i, but only for odd row widths — so you see 1234567, 12345, 123, 1.
Remember
Rule: for i = max..1 step -2, print 1..i
1234567
12345
123
1 ← max = 7
In PHP you solve it with an outer loop that steps by 2: for ($i = $max; $i >= 1; $i -= 2), an inner loop that prints each digit with echo $j, then echo PHP_EOL ends the row. Program 13 flips direction with $i % 2; this pattern always ascends but skips even widths.
Approach
How to Solve It
Outer loop counts down from an odd $max with step 2. Inner loop prints 1..$i. End each row with echo PHP_EOL.
Method
Idea
Best for
Step by 2
Outer $i -= 2 visits only odd widths
Learning, interviews, exams
Every width
Same inner loop with $i-- for comparison
Seeing what the custom step skips
Pseudocode
Pseudocode
for i from max down to 1 step 2:
for j from 1 to i:
print j (no newline)
print newline
Cheat sheet
Goal
Pattern
Walk odd widths
for ($i = $max; $i >= 1; $i -= 2)
Print 1..i
for ($j = 1; $j <= $i; $j++) echo $j;
End the row
echo PHP_EOL;
Force odd max
if ($max % 2 === 0) $max -= 1;
Every width (compare)
for ($i = $max; $i >= 1; $i--)
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j
Stays on the same line
Each digit in 1..i
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 maximum width and the odd-length triangle updates instantly — prefer odd values so the top row matches the classic demo.
Whole numbers from 1 to 9 (single digits). Odd values keep the classic odd-only shape.
Live resultmax = 7 · 16 digits
1234567
12345
123
1
Trace
Worked Walkthrough
Trace $max = 7 — each outer-loop value of $i, the inner range, and the printed row.
$i
Inner $j
Length
Printed row
7
1..7
7
1234567
5
1..5
5
12345
3
1..3
3
123
1
1..1
1
1
Total digits are 7+5+3+1 = 16 — for odd max n, that equals ((n+1)/2)².
Code
PHP Programs
Three complete programs: fixed odd max, CLI input, and an every-width comparison. Use View Output for sample results.
Example 1 — Fixed $max = 7
Hard-coded odd maximum — outer steps by 2, inner prints 1..i.
1. Only the step changed.$i-- visits every width; $i -= 2 in Example 1 skips the even ones.
2. Same inner rule. Digits still print 1..i with echo $j.
3. Compare side by side. Seeing both outputs makes the custom step easy to remember.
Edge Cases & Pitfalls
Check these before calling the solution done.
even max
Start with an odd maximum
An even start prints even lengths (6, 4, 2). Force odd with if ($max % 2 === 0) $max -= 1;.
wrong step
Use $i -= 2, not $i--
$i-- reprints the every-width triangle (see Example 3).
echo $i
echo $i instead of echo $j
That repeats the row length. This pattern prints the sequence value of $j.
PHP_EOL inside
echo PHP_EOL inside the inner loop
That puts each digit on its own line. Print digits without a newline; call echo PHP_EOL only after the row finishes.
max = 1
Single digit
Output is just 1 — a good sanity check.
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
Odd widths (Examples 1–2)
O(n²)
O(1)
Every width (Example 3)
O(n²)
O(1)
For odd max n you print 1+3+…+n digits — about half of the full triangle, but still O(n²).
Remember
Key Takeaways
Rule: for odd $i from $max down by 2, print digits 1..i.
Custom step:$i -= 2 is what skips even row lengths.
echo vs PHP_EOL: digits stay on the line; echo PHP_EOL advances after each row.
Next step: Program 15 builds an alternating binary triangle with 0 and 1.
One line: step $i down by 2 from an odd max, print 1..i with echo $j, then echo PHP_EOL.
Frequently Asked Questions
The outer loop uses $i -= 2, so it visits only odd widths: 7, 5, 3, 1. Even lengths like 6, 4, 2 are skipped.
Because $i -= 2 skips even row lengths. After 1234567 (7 digits), the next row is 5 digits (12345), not 6.
echo stays on the same line. echo PHP_EOL ends the current line. Digits use echo $j; the row break uses echo PHP_EOL after the inner loop.
Program 13 alternates ascending/descending direction with $i % 2 (12345, 4321, 123, 21, 1). Program 14 always prints 1..$i but only for odd row lengths using $i -= 2.
O(n²) where n is the maximum row width. You print about 1+3+5+…+n odd-width digits, which is still O(n²).
Subtract 1 to make it odd (e.g. if $max % 2 === 0 then $max -= 1) so the first row stays odd-length, or document that even input shifts the pattern.
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?
Only odd-length rows print. The outer loop uses $i −= 2 (7, 5, 3, 1) and the inner loop prints 1..$i — still O(n²) total digit prints for maximum width n.