PHP Descending Number Triangle Pattern (Left-Aligned)

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

What Is This Pattern?

A left-aligned descending number triangle starts every row at $rows and counts down to the current $i, so each line gets shorter from the right.

Remember
Rule: print rows..i  (no leading spaces)

54321
5432
543
54
5   ← 5 rows

In PHP the outer loop runs $i from 1 to $rows. The inner loop prints $j from $rows down to $i, then echo PHP_EOL ends the row.

How to Solve It

One nested-loop idea: ascending outer bound, descending inner digits.

MethodIdeaBest for
$rows..$i loopsStart each row at $rows; stop at $iLearning, interviews, exams
User-input rowsSame logic with a variable heightPractice / demos

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from rows down to i:
        print j
    print newline

Cheat sheet

GoalPattern
Raise the stop valuefor ($i = 1; $i <= $rows; $i++)
Print $rows..$ifor ($j = $rows; $j >= $i; $j--) echo $j;
Spaced digitsecho $j . " ";
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

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

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

Live Preview

Change the row count and the shrinking descending triangle updates instantly.

Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 digits
54321
5432
543
54
5

Worked Walkthrough — $rows = 4

Trace how the stop value $i shortens each row while every line still starts at 4.

$iDigits $jPrinted row
14 3 2 14321
24 3 2432
34 343
444

Row length is $rows - $i + 1. The first digit is always $rows.

PHP Programs

Three complete programs: fixed height, CLI input, and a compact 3-row demo. Use View Output for sample results.

Example 1 — Fixed $rows = 5

Ascending outer loop; each row prints from 5 down to the current $i.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = $rows; $j >= $i; $j--)
        echo $j;

    echo PHP_EOL;
}

How It Works

1. Outer loop. $i grows from 1 to 5 — the inner stop value rises, so rows get shorter.

2. Inner loop. $j always starts at 5 and counts down to $i — every row begins with 5.

3. Newline. echo PHP_EOL after the digits starts the next shorter row.

Example 2 — User Input

Read the row count from STDIN; the first digit of every row is still $rows.

PHP
<?php
echo "Enter rows: ";
$input = trim(fgets(STDIN));

if (!is_numeric($input) || (int) $input < 1) {
    echo "Please enter a positive whole number." . PHP_EOL;
    exit(1);
}

$rows = (int) $input;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = $rows; $j >= $i; $j--)
        echo $j;

    echo PHP_EOL;
}

How It Works

1. Validate rows. Check is_numeric; require $rows >= 1.

2. Same shape. Four rows all start at 4 and shrink to a single 4.

Example 3 — Compact $rows = 3

A smaller fixed demo — same $rows..$i idea, easier to trace by hand.

PHP
<?php
$rows = 3;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = $rows; $j >= $i; $j--)
        echo $j;

    echo PHP_EOL;
}

How It Works

1. Same rules. Outer raises $i; inner prints from 3 down to $i.

2. Quick check. The last row is a single 3.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong twin

Use Program 3’s outer loop

for ($i = $rows; $i >= 1; $i--) with $j = $i..1 prints 4321, 321… Keep ascending $i and inner $rows..$i.

full line

Loop $j down to 1 every row

That reprints 54321 five times. Stop at $j >= $i.

ascending $j

Print 1..$i instead

That builds Program 1’s ascending triangle. Keep $j from $rows down to $i.

rows = 1

Single digit

Output is just 1 — one value, one row.

rows ≤ 0

Empty output

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

Bad input

(int) alone is not enough

Prefer is_numeric after trim(fgets(STDIN)) so non-numeric input does not silently become 0.

Time and Space Complexity

ProgramTimeExtra space
Fixed / compact (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

Total printed digits are n + (n-1) + … + 1 = n(n+1)/2, which is quadratic in n.

Key Takeaways

  • Rule: every row prints $rows..$i — always starts at $rows.
  • Length: row $i has $rows - $i + 1 digits; no leading spaces.
  • echo vs PHP_EOL: digits stay on the line; echo PHP_EOL advances after each row.
  • Next step: Program 5 prints an ascending number triangle.

One line: for each row $i, print digits from $rows down to $i to build a left-aligned shrinking triangle.

Frequently Asked Questions

Because the inner loop always begins at $rows (the maximum digit) and counts down. Only the stopping point $i changes per row.
Row i prints $rows - $i + 1 digits — the inner loop runs from $j = $rows down to $i.
Program 3 prints $i..1 with a descending outer loop (4321, 321, ...). Program 4 prints $rows..$i with an ascending outer loop — every row starts at $rows.
No — digits are left-aligned with no leading spaces. Each row begins flush left at the maximum digit.
Replace 5 with $rows in the outer loop bound — see Example 2.
Use echo $j . " " instead of echo $j.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
Only one row prints — a single digit matching $rows.

Did you know?

Each row starts at $rows and counts down to $i. Row $i prints $rows - $i + 1 digits — total prints = n(n+1)/2; output is left-aligned with no leading spaces.

Next: Ascending Number Triangle

Print a left-aligned triangle that grows with ascending digits.

Program 5 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.

6 people found this page helpful