1. Same rules. Outer raises $i; inner prints from 3 down to $i.
2. Quick check. The last row is a single 3.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong twin
Use Program 3’s outer loop
for ($i = $rows; $i >= 1; $i--) with $j = $i..1 prints 4321, 321… Keep ascending $i and inner $rows..$i.
full line
Loop $j down to 1 every row
That reprints 54321 five times. Stop at $j >= $i.
ascending $j
Print 1..$i instead
That builds Program 1’s ascending triangle. Keep $j from $rows down to $i.
rows = 1
Single digit
Output is just 1 — one value, one row.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
Bad input
(int) alone is not enough
Prefer is_numeric after trim(fgets(STDIN)) so non-numeric input does not silently become 0.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
Total printed digits are n + (n-1) + … + 1 = n(n+1)/2, which is quadratic in n.
Remember
Key Takeaways
Rule: every row prints $rows..$i — always starts at $rows.
Length: row $i has $rows - $i + 1 digits; no leading spaces.
echo vs PHP_EOL: digits stay on the line; echo PHP_EOL advances after each row.
Next step: Program 5 prints an ascending number triangle.
One line: for each row $i, print digits from $rows down to $i to build a left-aligned shrinking triangle.
Frequently Asked Questions
Because the inner loop always begins at $rows (the maximum digit) and counts down. Only the stopping point $i changes per row.
Row i prints $rows - $i + 1 digits — the inner loop runs from $j = $rows down to $i.
Program 3 prints $i..1 with a descending outer loop (4321, 321, ...). Program 4 prints $rows..$i with an ascending outer loop — every row starts at $rows.
No — digits are left-aligned with no leading spaces. Each row begins flush left at the maximum digit.
Replace 5 with $rows in the outer loop bound — see Example 2.
Use echo $j . " " instead of echo $j.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
Only one row prints — a single digit matching $rows.
🤔
Did you know?
Each row starts at $rows and counts down to $i. Row $i prints $rows - $i + 1 digits — total prints = n(n+1)/2; output is left-aligned with no leading spaces.