PHP Number Pattern (Increasing Suffix)

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

What Is This Pattern?

An increasing suffix pattern starts at the largest digit and grows leftward each row — 5, then 45, then 345, until the full run 1..$rows.

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

5
45
345
2345
12345     ← rows = 5

Unlike Program 5 (prints 1..$i growing from the left), here the outer loop counts down so each row prepends a smaller start digit. Program 7 prints a growing reverse triangle (1, 21, 321).

How to Solve It

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

MethodIdeaBest for
Fixed rowsHard-code height; descending outer + $i..$rowsLabs 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 i to rows:
        print j
    print newline

Cheat sheet

GoalPattern
Set size$rows = 5;
Outer (start digit)for ($i = $rows; $i >= 1; $i--)
Inner (suffix)for ($j = $i; $j <= $rows; $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 suffix with echo, then break once.

Live Preview

Change the row count and the increasing suffix 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
45
345
2345
12345

Worked Walkthrough

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

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

Earlier rows ($i = 5, 4) printed 5 and 45. Later rows prepend 2 then 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 $i..$rows.

PHP
<?php
$rows = 5;

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

How It Works

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

2. Grow left. Each step lowers $i, so the row gains one digit on the left (45, 345, …).

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 = $i; $j <= $rows; $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 $i..$rows inner match Example 1 — only the source of $rows changes.

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

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 = $i; $j <= $rows; $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.

wrong outer

Counting the outer loop up instead of down

That prints the longest row first (12345, then 2345…) — Program 2’s shape. Keep for ($i = $rows; $i >= 1; $i--).

wrong inner

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

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

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 $i..$rows — the line grows on the left.
  • Vs Program 5: that grows 1..$i; this always ends at $rows.
  • echo vs PHP_EOL: digits stay on the line; echo PHP_EOL advances after the inner loop.
  • Next step: Program 7 prints 1, 21, 321 with a countdown inner loop.

One line: count the start digit down; always print through $rows.

Frequently Asked Questions

On the first iteration $i equals $rows, so the inner loop prints only $rows (just 5). Each next row starts one value earlier, so the line grows on the left.
The outer loop runs $i from $rows down to 1. For each row $i, the inner loop runs $j from $i to $rows and echoes $j, then echo PHP_EOL ends the row.
Program 2 prints 12345, then 2345, then 345 (outer loop counts up). Program 6 prints 5, then 45, then 345 (outer loop counts down) — same inner loop, reversed row order.
Program 5 prints 1, 12, 123 (inner loop $j = 1..$i). Program 6 prints 5, 45, 345 (inner loop $j = $i..$rows with descending outer loop).
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?

The outer loop starts at $rows and counts down — each row prints digits from $i through $rows, so the suffix grows on the left: 5, 45, 345, …

Next: Growing Reverse Triangle

Continue with rows like 1, 21, 321.

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