PHP Number Pattern (Reverse Growing)

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

What Is This Pattern?

A reverse growing number pattern starts at the largest digit and grows rightward each row — 5, then 54, then 543, until the full countdown $rows..1.

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

5
54
543
5432
54321     ← rows = 5

Unlike Program 7 (prints $i..1 growing from 1), here every row starts at $rows and stops at the current $i. Program 9 continues with repeated digits (1, 22, 333).

How to Solve It

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

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

Pseudocode

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

Cheat sheet

GoalPattern
Set size$rows = 5;
Outer (stop digit)for ($i = $rows; $i >= 1; $i--)
Inner (from rows)for ($j = $rows; $j >= $i; $j--) echo $j;
End the rowecho PHP_EOL;
Digits on row i$rows - $i + 1

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 reverse growing pattern 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
5
54
543
5432
54321

Worked Walkthrough

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

StepLoopResult
Outer$i = 3 (counting down from 5)Stop digit = 3
Inner$j = 5..3543
Breakecho PHP_EOLNext row starts

Earlier rows ($i = 5, 4) printed 5 and 54. Later rows extend further toward 1.

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 down; inner prints $rows..$i.

PHP
<?php
$rows = 5;

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

How It Works

1. Start short. When $i = 5, the inner loop prints only 5.

2. Grow right. Each step lowers $i, so the row gains one more digit (54, 543, …).

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 = $rows; $i >= 1; $i--) {
    for ($j = $rows; $j >= $i; $j--) {
        echo $j;
    }
    echo PHP_EOL;
}
?>

How It Works

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

2. Same loops. Descending outer and $rows..$i 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 = $rows; $i >= 1; $i--) {
    for ($j = $rows; $j >= $i; $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.

Program 7 loops

Using $i = 1..$rows and $j = $i..1

That prints Program 7 (1, 21, 321). Keep outer $rows..1 and inner $rows..$i.

wrong inner

Starting the inner loop at $i instead of $rows

Each row must begin at $rows. Use for ($j = $rows; $j >= $i; $j--).

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 $rows..1, inner $rows..$i — every row starts at $rows.
  • Vs Program 7: that grows from 1 downward; this always opens with $rows.
  • echo vs PHP_EOL: digits stay on the line; echo PHP_EOL advances after the inner loop.
  • Next step: Program 9 repeats the row digit (1, 22, 333).

One line: start each row at $rows and stop at the current $i.

Frequently Asked Questions

When $i = $rows, the inner loop runs $j from $rows down to $i ($rows), so only one digit prints. Each later row lowers $i, so more digits appear: 54, 543, and so on.
The outer loop runs $i from $rows down to 1. For each $i, the inner loop runs $j from $rows down to $i and echoes $j, then echo PHP_EOL ends the row.
Program 4 shrinks each row from $rows down to $i (54321, 5432, 543). Program 8 grows each row from one digit up to $rows digits (5, 54, 543, 5432, 54321) using the same inner range $j = $rows..$i.
Program 7 uses outer $i = 1..$rows and inner $j = $i..1 (1, 21, 321). Program 8 uses outer $i = $rows..1 and inner $j = $rows..$i (5, 54, 543).
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 counts $i down from $rows; inner loop prints $j from $rows down to $i — 5, 54, 543, … O(n²) for $n rows.

Next: Repeated Number Triangle

Continue with rows like 1, 22, 333.

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