PHP Reverse Number Triangle Pattern (Growing)

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

What Is This Pattern?

A growing reverse number triangle prints each row from the row index down to 1 — 1, then 21, then 321, until $rows..1 on the last line.

Remember
Rule: for i = 1..rows
        print digits i..1

1
21
321
4321
54321     ← rows = 5

Unlike Program 5 (prints 1..$i ascending) and Program 6 (suffix growing leftward), here the outer loop counts up and the inner loop counts down. Program 8 continues with 5, 54, 543.

How to Solve It

Outer loop: $i from 1 to $rows. Inner loop: echo $j from $i down to 1. End each row with echo PHP_EOL.

MethodIdeaBest for
Fixed rowsHard-code height; outer up, inner downLabs and demos
CLI inputSame loops; read $rows at runtimeInteractive practice
Spaced digitsecho $j . " " between numbersEasier reading

Pseudocode

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

Cheat sheet

GoalPattern
Set size$rows = 5;
Outer (row length)for ($i = 1; $i <= $rows; $i++)
Inner (countdown)for ($j = $i; $j >= 1; $j--) echo $j;
End the rowecho PHP_EOL;
Digits on row i$i digits

Printing Numbers vs Starting a New Line

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

Build the full countdown with echo, then break once.

Live Preview

Change the row count and the growing reverse triangle updates instantly.

Whole numbers from 3 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · digits = 15
1
21
321
4321
54321

Worked Walkthrough

Trace $rows = 5, row $i = 3 — the inner loop builds 321.

StepLoopResult
Outer$i = 3Row length = 3
Inner$j = 3..1321
Breakecho PHP_EOLNext row starts

Earlier rows printed 1 and 21. Later rows prepend a larger start digit (4, then 5).

PHP Programs

Three complete programs: fixed $rows = 5, CLI input, and spaced digits. Use View Output for sample results.

Example 1 — Fixed $rows = 5

Hard-coded height — outer loop counts up; inner prints $i..1.

PHP
<?php
$rows = 5;

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

How It Works

1. Grow the row. When $i = 1, the inner loop prints only 1.

2. Count down. When $i = 3, $j runs 3, 2, 1 → 321.

3. Row break. echo PHP_EOL after the inner loop starts the next line.

Example 2 — User Input

Read $rows from the CLI. Prefer is_numeric (shown below).

PHP
<?php
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
    echo "Enter a positive whole number." . PHP_EOL;
    exit(1);
}
$rows = (int) $input;

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

How It Works

1. Validate input. is_numeric rejects letters; require a positive whole number.

2. Same loops. Ascending outer and $i..1 inner match Example 1 — only the source of $rows changes.

3. Scale. For $rows = 4, the last line is 4321.

Example 3 — Spaced Digits

Same logic; print a space after each digit for easier reading.

PHP
<?php
$rows = 5;

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

How It Works

1. Same shape. Outer and inner bounds are unchanged.

2. Spacing. echo $j . " " separates digits without changing the sequence.

3. Still one break. echo PHP_EOL after the inner loop ends the row.

Edge Cases & Pitfalls

Check these before calling the solution done.

ascending inner

Using $j = 1..$i like Program 5

That prints 1, 12, 123. For this pattern keep $j = $i..1.

wrong outer

Counting the outer loop down

That changes the triangle order. Keep for ($i = 1; $i <= $rows; $i++).

PHP_EOL inside

echo PHP_EOL inside the inner loop

That prints one digit per line. Call echo PHP_EOL only after the inner loop finishes.

rows = 1

Single digit

Output is one line: 1.

rows ≤ 0

Empty output

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

multi-digit

$rows > 9

echo 10 prints two characters and changes visual width. Labs usually stick to 1–9.

Time and Space Complexity

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

There are n rows, and total digits printed are 1 + 2 + … + n = n(n+1)/2, so work is O(n²).

Key Takeaways

  • Rule: outer 1..$rows, inner $i..1 — each row is a countdown.
  • Vs Program 5: same triangle height; reverse the inner loop direction.
  • echo vs PHP_EOL: digits stay on the line; echo PHP_EOL advances after the inner loop.
  • Next step: Program 8 prints 5, 54, 543 with a descending outer loop.

One line: grow the row length, then print from $i down to 1.

Frequently Asked Questions

Each row must print digits from $i down to 1. Using $j-- after starting at $j = $i gives 321 on row 3 instead of 123.
The outer loop runs $i from 1 to $rows. For each row $i, the inner loop runs $j from $i down to 1 and echoes $j, then echo PHP_EOL ends the row.
Program 5 prints 1, 12, 123 (inner loop $j = 1..$i ascending). Program 7 prints 1, 21, 321 (same outer loop, inner loop $j = $i..1 descending).
Program 6 uses a descending outer loop and prints 5, 45, 345 (inner $j = $i..$rows). Program 7 uses an ascending outer loop and counts $j down on each row (1, 21, 321).
Yes. Use echo $j . " " inside the inner loop — see Example 3.
echo stays on the same line for each digit. echo PHP_EOL ends the row after the inner loop finishes.
O(n²) for n rows. Total printed digits are 1+2+...+n = n(n+1)/2.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Outer loop grows row index $i; inner loop prints $j from $i down to 1 — 1, 21, 321, … O(n²) for $n rows.

Next: Reverse Growing Pattern

Continue with rows like 5, 54, 543.

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