PHP Descending Number Triangle Pattern (Odd Length)

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

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.

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.

MethodIdeaBest for
Step by 2Outer $i -= 2 visits only odd widthsLearning, interviews, exams
Every widthSame inner loop with $i-- for comparisonSeeing 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

GoalPattern
Walk odd widthsfor ($i = $max; $i >= 1; $i -= 2)
Print 1..ifor ($j = 1; $j <= $i; $j++) echo $j;
End the rowecho PHP_EOL;
Force odd maxif ($max % 2 === 0) $max -= 1;
Every width (compare)for ($i = $max; $i >= 1; $i--)

Printing Numbers vs Starting a New Line

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

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

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 result max = 7 · 16 digits
1234567
12345
123
1

Worked Walkthrough

Trace $max = 7 — each outer-loop value of $i, the inner range, and the printed row.

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

Total digits are 7+5+3+1 = 16 — for odd max n, that equals ((n+1)/2)².

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.

PHP
<?php
$max = 7;

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

How It Works

1. Outer skips even widths. $i -= 2 visits 7, 5, 3, 1 only.

2. Inner prints ascending digits. $j runs from 1 to $i with echo $j.

3. End the row. Call echo PHP_EOL; only after the inner loop finishes.

Example 2 — User Input

Read the maximum width from the CLI. Prefer an odd value so the top row matches the classic demo.

PHP
<?php
echo "Enter an odd maximum (e.g. 9): ";
$max = (int) trim(fgets(STDIN));

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

How It Works

1. Same loop core. Only the source of $max changes from a literal to CLI input.

2. Entering 5 stops early. You get three odd-length rows ending at 1.

3. Safer input tip. Validate and force an odd top width:

Safer input
echo "Enter an odd maximum (e.g. 9): ";
$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 — Every Width ($i--)

Same inner loop with a normal step — includes even lengths for comparison.

PHP
<?php
$max = 7;

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

How It Works

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.

Time and Space Complexity

ProgramTimeExtra 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²).

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.

Next: Alternating Binary Triangle

Continue with rows built from alternating 0 and 1.

Program 15 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