PHP Repeating Number Triangle Pattern (Descending)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A descending repeating number triangle prints digit i repeated rows - i + 1 times, counting i down from rows.
Remember
Rule: for i = n..1, print i exactly (n - i + 1) times
5
44
333
2222
11111 ← 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…).
Approach
How to Solve It
Outer loop counts down from $rows to 1. Inner loop repeats digit $i exactly $rows - $i + 1 times. End each row with echo PHP_EOL.
Method
Idea
Best for
Nested loops
Outer picks digit n..1; inner repeats it n-i+1 times
Learning, interviews, exams
str_repeat
Build each full row in one call
Leaner demos once loops click
Pseudocode
Pseudocode
for i from rows down to 1:
for j from rows down to i:
print i (no newline)
print newline
1. Count still matters.$rows - $i + 1 is the same repeat length the inner loop used.
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 ($i = 1; $i <= $rows; $i++) with echo $i × $i
That is Program 9 (1, 22, 333). Keep counting down and growing repeats for this pattern.
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.
wrong count
$j <= $i instead of $j >= $i
The inner bound must be for ($j = $rows; $j >= $i; $j--) so repeat count equals $rows - $i + 1.
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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 1+2+…+n digits, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: for $i = n..1, print digit $i exactly n - i + 1 times.
Inner bound:$j from $rows down to $i grows the repeat count as the digit shrinks.
echo vs PHP_EOL: repeats stay on the line; echo PHP_EOL advances after each row.
Next step: Program 11 inverts the triangle (longest row of the top digit first).
One line: for each digit from n down to 1, print it n-i+1 times with echo, then echo PHP_EOL.
Frequently Asked Questions
The outer loop decreases i from rows down to 1. The inner loop runs j from rows down to i, so it executes once when i equals rows, twice when i is rows-1, and so on until rows times for i = 1.
for ($j = $rows; $j >= $i; $j--) runs ($rows - $i + 1) times. That count grows as i shrinks, which is why 4 prints twice, 3 prints three times, and 1 prints rows times.
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 9 grows upward: row i repeats digit i exactly i times (1, 22, 333). Program 10 starts with one copy of the top digit and increases repeats as the digit decreases (5, 44, 333, 2222, 11111).
O(n²) where n is the number of rows. Total echo calls equal 1+2+…+n = n(n+1)/2.
Yes. echo str_repeat((string)$i, $rows - $i + 1) . 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 rows - i + 1 times. The outer loop counts down from rows, so the first row shows one copy of the top digit and the last row repeats 1 for rows times — still O(n²) total prints.