Shape Rule
$i..$rows per row
Row 1 prints 5, row 2 prints 45, row 3 prints 345, growing until the full line 12345.

The increasing suffix number pattern grows each row by one digit on the left — a natural step after the ascending triangle in Program 5. This tutorial covers the shape rule, descending outer loop, live preview, algorithm steps, worked PHP examples, edge cases, and complexity.
$i..$rows per row
Row 1 prints 5, row 2 prints 45, row 3 prints 345, growing until the full line 12345.
$rows..1
for ($i = $rows; $i >= 1; $i--) picks the starting digit for each row.
Start at $i
for ($j = $i; $j <= $rows; $j++) prints digits from $i through $rows.
Same line / next line
Digits use echo $j; end each row with echo PHP_EOL.
1–20 rows
Pick a row count and draw the increasing suffix pattern instantly in the browser.
Complexity
Total digit prints = n(n+1)/2; extra memory stays O(1).
An increasing suffix number pattern starts with a single digit and adds one more digit on the left each row. With $rows = 5, the output is 5, 45, 345, 2345, 12345.
In PHP the outer loop counts down from $rows to 1, the inner loop prints $j from $i to $rows, then echo PHP_EOL moves to the next line.
It teaches how reversing the outer loop order reshapes Program 2’s output — a key step after Program 5.
On row $i, print digits $i through $rows.
for ($j = $i; $j <= $rows; $j++) — not $j = 1.
echo $j in the inner loop; echo PHP_EOL after.
Follow Program 5; compare with Program 2; continue to Program 7 (growing reverse triangle).
In short: for each row $i from $rows down to 1, print digits $i..$rows with echo $j, then call echo PHP_EOL.
Given a positive integer $rows, print an increasing suffix pattern: each row $i shows digits from $i through $rows, with the outer loop counting from $rows down to 1.
// $rows = 5 (conceptual shape)
for ($i = $rows; $i >= 1; $i--) {
for ($j = $i; $j <= $rows; $j++) {
echo $j; // digits $i..$rows
}
echo PHP_EOL; // next row
} | Item | Type | Description |
|---|---|---|
$rows | int | Number of triangle lines to print (typically ≥ 1). |
| Printed output | text | Each row prints $i..$rows; the first row has one digit, the last row has $rows digits. |
for $i from $rows down to 1:
for $j from $i to $rows:
echo $j (no newline)
echo PHP_EOL | Approach | Idea | Best for |
|---|---|---|
| Nested loops | Outer rows + inner digits | Learning and interviews |
| Spaced output | echo $j . " " | Easier reading per row — Example 3 |
| Goal | Pattern |
|---|---|
| Walk each row | for ($i = $rows; $i >= 1; $i--) |
Print digits $i..$rows | for ($j = $i; $j <= $rows; $j++) echo $j; |
| End the row | echo PHP_EOL; |
| Spaced digits | echo $j . " " |
| Program 2 contrast | Program 2 outer $i = 1..$rows; Program 6 outer $i = $rows..1 |
| Program 5 contrast | Program 5 uses inner $j = 1..$i; Program 6 uses inner $j = $i..$rows |
How descending outer loop and inner $j = $i..$rows work together.
for ($i = $rows; $i >= 1; $i--)Counts down the starting digit — shortest row prints first.
for ($j = $i; $j <= $rows; $j++)Prints from current start $i up to $rows.
echo $j . " "Adds spaces between digits — see Example 3.
trace $i=3Dry-run when $i = 3: prints 345 on that row.
Reach for this pattern when teaching how the inner loop start value changes the shape.
Natural follow-up after the ascending triangle — now the suffix grows on the left.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare Program 2 (same inner loop, outer counts up) and Program 7 (reverse count) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Enter a row count and draw the increasing suffix pattern in the browser.
Three complete PHP programs — fixed $rows = 5, fgets(STDIN) input, and a spaced-output variant. Click View Output to reveal sample console results.
Print five rows of the increasing suffix pattern with nested loops.
$rows = 5Hard-coded height — ideal for first demos and screenshots.
<?php
$rows = 5;
for ($i = $rows; $i >= 1; $i--) {
for ($j = $i; $j <= $rows; $j++) {
echo $j;
}
echo PHP_EOL;
} When $i = 5, the inner loop prints only 5. When $i = 4, it prints 45, and so on until $i = 1 prints 12345. echo PHP_EOL after the inner loop starts the next row.
Let the user choose the height at runtime.
Read the row count with fgets(STDIN) (validate with is_numeric($input) first in real apps).
<?php
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . PHP_EOL;
exit(1);
}
$rows = (int) $input;
for ($i = $rows; $i >= 1; $i--) {
for ($j = $i; $j <= $rows; $j++) {
echo $j;
}
echo PHP_EOL;
} Same nested-loop core as Example 1; only the source of $rows changes. Non-numeric input leaves $rows unset if you ignore fgets(STDIN)’s return value — always check it in safer labs.
Add spaces between digits for easier reading.
Print a space after each digit with echo $j . " ".
<?php
$rows = 5;
for ($i = $rows; $i >= 1; $i--) {
for ($j = $i; $j <= $rows; $j++) {
echo $j . " ";
}
echo PHP_EOL;
} Same $j = $i..$rows logic with descending outer loop; only the output format adds spaces between digits.
echo is built in; use fgets(STDIN) when reading input. Set $rows (fixed or from input).
for ($i = $rows; $i >= 1; $i--) selects the starting digit for each row.
for ($j = $i; $j <= $rows; $j++) prints digits $i..$rows with echo $j.
echo PHP_EOL ends the row so the next outer iteration starts fresh.
Total digit prints: 1+2+…+n = n(n+1)/2 — O(n²) time, O(1) extra memory.
$i = 3 (when $rows = 5)Trace outer-loop value $i = 3 and the inner $j range on that row.
$j | Action | Row so far |
|---|---|---|
| 3 | print 3 | 3 |
| 4 | print 4 | 34 |
| 5 | print 5 | 345 |
After the inner loop, echo PHP_EOL moves to the next row ($i = 2 will print 2345).
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change $j <= $rows and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: flip outer loop to count up and compare with Program 2.
Practice echo vs row newline without complex math.
Example: put echo PHP_EOL inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: use echo $j . " " for spaced digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 → 55.
Pair the pattern with fgets(STDIN) return checks and positive-row checks.
Example: reject $rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner PHP courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the descending outer loop first; then try fgets(STDIN) input and spaced output in Example 3.
Small habits that keep number-pattern code clean.
Use $rows (or $n) and keep $i/$j for row/column — or rename to row/col.
fgets(STDIN)Call is_numeric($input) so bad input does not leave $rows uninitialized.
echo PHP_EOL OutsideOnly call echo PHP_EOL after the inner loop finishes the row.
for ($i = $rows; $i >= 1; $i--) prints the shortest row first — key to this pattern.
$i/h3>for ($j = $i; $j <= $rows; $j++) — trace $rows = 3 on paper before larger demos.
Pro Tip: if the output is a vertical list of single digits, you almost certainly put echo PHP_EOL inside the inner loop.
Mistakes that commonly break increasing suffix number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use echo $j for digits; echo PHP_EOL only after the inner loop.
$j = 1 prints digits from 1 each row — a different shape, not the increasing suffix pattern.
→ For this shape, keep for ($j = $i; $j <= $rows; $j++).
Omitting echo PHP_EOL glues every digit onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input leave $rows uninitialized.
→ Prefer is_numeric($input) and re-prompt on failure.
Switching to $i = 0 without adjusting the inner bound prints an empty first row or wrong counts.
→ If 0-based, start inner at $j = $i and end at $rows - 1 or adjust bounds.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
$rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n²/2 characters — fine for labs, noisy for huge n.
Unchecked fgets(STDIN) leaves $rows unset — call is_numeric($input) first.
Try echo $j . " " for spaces between numbers.
Try these variations to lock in the pattern.
12345, 2345… (outer counts up)for ($i = 1; $i <= $rows; $i++)1, 21, 321…echo $j . " " between digitsn(n+1)/2 — hence O(n²) time.echo $j stays on the line; echo PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 should print a single 1.$j = 1..$i).Quick Takeaway: outer loop $i = $rows..1, inner loop prints digits $i..$rows, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Spaced output (Example 3) | O(n²) | O(1) |
The increasing suffix number pattern is a compact nested-loop lesson: outer loop counts down while the inner loop prints digits $i through $rows. Master the fixed-$rows version, then try user input and spaced output.
Practice the three examples above, then continue to Program 7 for the growing reverse number triangle.
Row $i prints $i..$rows — keep echo $j for digits and echo PHP_EOL for the break, and validate row counts when reading input.
for ($i = $rows; $i >= 1; $i--) in the outer loopfor ($j = $i; $j <= $rows; $j++) prints digits $i..$rowsecho $j for digits and echo PHP_EOL after each row$rows ≥ 1 for interactive programsis_numeric($input) before using $rowsecho PHP_EOL inside the inner digit loop$j = 1 when you meant increasing suffix shape$rows = 1 edge casePrint the pattern the beginner-friendly way.
Row $i prints $i..$rows
Counts down $rows
$j = $i, not $j = 1
Ends each row
I/OO(n²) time
AnalysisThe 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, …
Move on to the growing reverse number triangle in the PHP number-pattern series.
12 people found this page helpful