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).
Approach
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.
Method
Idea
Best for
Fixed rows
Hard-code height; outer down, inner $rows..$i
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 rows down to 1:
for j from rows down to i:
print j
print newline
Cheat sheet
Goal
Pattern
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 row
echo PHP_EOL;
Digits on row i
$rows - $i + 1
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 reverse growing pattern 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.
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.
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 $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.