An increasing suffix pattern starts at the largest digit and grows leftward each row — 5, then 45, then 345, until the full run 1..$rows.
Remember
Rule: for i = rows down to 1
print digits i..rows
5
45
345
2345
12345 ← rows = 5
Unlike Program 5 (prints 1..$i growing from the left), here the outer loop counts down so each row prepends a smaller start digit. Program 7 prints a growing reverse triangle (1, 21, 321).
Approach
How to Solve It
Outer loop: $i from $rows down to 1. Inner loop: echo $j from $i to $rows. End each row with echo PHP_EOL.
Method
Idea
Best for
Fixed rows
Hard-code height; descending outer + $i..$rows
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 i to rows:
print j
print newline
Cheat sheet
Goal
Pattern
Set size
$rows = 5;
Outer (start digit)
for ($i = $rows; $i >= 1; $i--)
Inner (suffix)
for ($j = $i; $j <= $rows; $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 suffix with echo, then break once.
Try it
Live Preview
Change the row count and the increasing suffix 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.
wrong outer
Counting the outer loop up instead of down
That prints the longest row first (12345, then 2345…) — Program 2’s shape. Keep for ($i = $rows; $i >= 1; $i--).
wrong inner
Using $j = 1..$i like Program 5
That prints 1, 12, 123. For this pattern keep $j = $i..$rows.
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 $i..$rows — the line grows on the left.
Vs Program 5: that grows 1..$i; this always ends at $rows.
echo vs PHP_EOL: digits stay on the line; echo PHP_EOL advances after the inner loop.
Next step: Program 7 prints 1, 21, 321 with a countdown inner loop.
One line: count the start digit down; always print through $rows.
Frequently Asked Questions
On the first iteration $i equals $rows, so the inner loop prints only $rows (just 5). Each next row starts one value earlier, so the line grows on the left.
The outer loop runs $i from $rows down to 1. For each row $i, the inner loop runs $j from $i to $rows and echoes $j, then echo PHP_EOL ends the row.
Program 2 prints 12345, then 2345, then 345 (outer loop counts up). Program 6 prints 5, then 45, then 345 (outer loop counts down) — same inner loop, reversed row order.
Program 5 prints 1, 12, 123 (inner loop $j = 1..$i). Program 6 prints 5, 45, 345 (inner loop $j = $i..$rows with descending outer loop).
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?
The outer loop starts at $rows and counts down — each row prints digits from $i through $rows, so the suffix grows on the left: 5, 45, 345, …