PHP Repeating Number Triangle Pattern (Inverted)

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

What Is This Pattern?

An inverted repeating number triangle prints digit i repeated exactly i times, counting i down from rows.

Remember
Rule: for i = n..1, print i exactly i times

55555
4444
333
22
1   ← 5 rows

In PHP you solve it with two nested for loops: the outer loop picks the digit, the inner loop repeats it with echo $i, then echo PHP_EOL ends the row. Program 9 is the ascending twin (1, 22, 333…); Program 10 grows repeats while the digit falls (5, 44, 333…).

How to Solve It

Outer loop counts down from $rows to 1. Inner loop repeats digit $i exactly $i times. End each row with echo PHP_EOL.

MethodIdeaBest for
Nested loopsOuter picks digit n..1; inner repeats it i timesLearning, interviews, exams
str_repeatBuild each full row in one callLeaner demos once loops click

Pseudocode

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

Cheat sheet

GoalPattern
Pick digit (count down)for ($i = $rows; $i >= 1; $i--)
Repeat digit $ifor ($j = 1; $j <= $i; $j++) echo $i;
End the rowecho PHP_EOL;
str_repeat formecho str_repeat((string) $i, $i) . PHP_EOL;
Program 10 twinfor ($j = $rows; $j >= $i; $j--) echo $i; (grows repeats)

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $iStays on the same lineEach repeat of digit $i
echo PHP_EOLEnds the current lineAfter the inner loop

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

Live Preview

Change the row count and the inverted triangle updates instantly — capped at 9 so every digit stays a single character.

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

Live result 5 rows · 15 digits
55555
4444
333
22
1

Worked Walkthrough

Trace $rows = 4 — each outer-loop value of $i, how many times the inner loop runs, and the printed row.

$iInner $jRepeatsPrinted row
41..444444
31..33333
21..2222
11..111

Total digits are 4+3+2+1 = 10 — the triangular number n(n+1)/2.

PHP Programs

Three complete programs: fixed height, CLI input, and a str_repeat shortcut. Use View Output for sample results.

Example 1 — Fixed $rows = 5

Outer loop picks the digit; inner loop repeats it exactly $i times.

PHP
<?php
$rows = 5;

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

How It Works

1. Count down. for ($i = $rows; $i >= 1; $i--) yields 5, 4, 3, 2, 1 — the digit for each row.

2. Match repeats to digit. When $i = 5, the inner loop runs five times (55555); when $i = 1, it runs once (1).

3. Newline. echo PHP_EOL; after the inner loop starts the next shorter row of a smaller digit.

Example 2 — User Input

Read the row count from the CLI. Prefer is_numeric in real apps (shown in the tip below).

PHP
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));

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

How It Works

1. Read rows. Ask for a row count, then trim(fgets(STDIN)) and cast to int.

2. Same nested loops. Four rows start at 4444 and end at 1.

3. Safer input tip. Bare (int) casts turn letters into 0. Prefer:

Safer input
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;

Example 3 — str_repeat Shortcut

Same shape with one call per row — best once the “digit equals count” rule is clear.

PHP
<?php
$rows = 5;

for ($i = $rows; $i >= 1; $i--) {
    echo str_repeat((string) $i, $i) . PHP_EOL;
}
?>

How It Works

1. Count still equals digit. str_repeat((string) $i, $i) builds the same row the inner loop printed.

2. Same shape. Identical output to Example 1 — keep the nested-loop version for exams that expect two loops.

3. Cast to string. str_repeat needs a string — (string) $i keeps the digit clear.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong twin

for ($j = $rows; $j >= $i; $j--)

That is Program 10 (5, 44, 333). Here digit and count both equal $i.

PHP_EOL inside

echo PHP_EOL inside the inner loop

That puts each digit on its own line. Print repeats without a newline; call echo PHP_EOL only after the row finishes.

count up

for ($i = 1; $i <= $rows; $i++) with echo $i × $i

That is Program 9 (1, 22, 333). Keep counting down so the widest row comes first.

rows = 1

Single digit

Output is just 1 — a good sanity check.

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. Labs usually stick to 1–9 for a clean single-digit look.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(n²)O(1)
str_repeat shortcut (Example 3)O(n²)O(1) beyond the row string

There are n rows printing n+(n-1)+…+1 digits, so total work is n(n+1)/2.

Key Takeaways

  • Rule: for $i = n..1, print digit $i exactly $i times.
  • Inner bound: $j from 1 to $i so digit and count stay equal.
  • echo vs PHP_EOL: repeats stay on the line; echo PHP_EOL advances after each row.
  • Next step: Program 12 shrinks while counting up (11111, 2222, 333…).

One line: for each digit from n down to 1, print it i times with echo, then echo PHP_EOL.

Frequently Asked Questions

The outer loop decreases i from rows down to 1, and the inner loop runs j from 1 to i. Row i repeats digit i exactly i times, so the widest row is first and each next row is shorter.
for ($j = 1; $j <= $i; $j++) echo $i runs the inner body i times. When i is 5 you get 55555; when i is 1 you get a single 1.
echo stays on the same line. echo PHP_EOL ends the current line. Repeated digits use echo $i; the row break uses echo PHP_EOL after the inner loop.
Program 10 grows repeat count as the digit shrinks (5, 44, 333, 2222, 11111) using for ($j = $rows; $j >= $i; $j--). Program 11 repeats digit i exactly i times (55555, 4444, 333, 22, 1) using for ($j = 1; $j <= $i; $j++).
O(n²) where n is the number of rows. Total echo calls equal n+(n-1)+…+1 = n(n+1)/2.
Yes. echo str_repeat((string)$i, $i) . PHP_EOL prints a full row in one call. Nested loops are better for learning; str_repeat is a handy shortcut once the bounds make sense.
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?

Row digit i repeats exactly i times. The outer loop counts down from rows, so the first row is widest (rows copies of rows) and each line shortens — still O(n²) total prints.

Next: Shrinking Repeating Pattern

Continue with the pattern that counts up while rows get shorter.

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