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.
Approach
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.
Method
Idea
Best for
Fixed rows
Hard-code height; outer up, inner down
Labs and demos
CLI input
Same loops; read $rows at runtime
Interactive practice
Spaced digits
echo $j . " " between numbers
Easier reading
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i down to 1:
print j
print newline
Cheat sheet
Goal
Pattern
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 row
echo PHP_EOL;
Digits on row i
$i digits
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j
Stays on the same line
Each digit in the row
echo PHP_EOL
Ends the current line
After the inner loop finishes
Build the full countdown with echo, then break once.
Try it
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.
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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²).
Remember
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.