Shape Rule
$rows..$i per row
Each row prints $j from $rows down to $i with no spaces (e.g. when $i = 3 → 543).

The Reverse Growing Number Pattern prints 5, 54, 543, 5432, 54321 — each row prints digits from $rows down to $i. This tutorial covers nested-loop logic, live preview, worked PHP examples, edge cases, and O(n²) complexity.
$rows..$i per row
Each row prints $j from $rows down to $i with no spaces (e.g. when $i = 3 → 543).
$i = $rows..1
Outer loop lowers $i; inner loop runs $j = $rows..$i.
No spaces
echo $j concatenates digits on the same row.
Series base
Stepping stone toward Floyd’s triangle, stars, and alphabet patterns.
1–20 rows
Pick a row count and draw the pattern instantly in the browser.
Complexity
Total digits ≈ n(n+1)/2 — quadratic time; extra memory stays O(1).
A reverse growing number pattern grows each row by one digit on the right: 5, 54, 543, through 54321 for five rows. Row with index $i prints digits from $rows down to $i.
In PHP the outer loop runs $i = $rows..1, the inner loop prints $j from $rows down to $i, then echo PHP_EOL moves to the next line.
It is a foundational nested-loop exercise — compare with Program 7 (growing reverse triangle) and continue to Program 9 (repeated number triangle).
Each row starts at $rows and stops at $i.
Inner loop $j = $rows..$i lengthens each row.
Program 4 shrinks rows (54321, 5432); Program 8 grows rows (5, 54, 543) with the same inner $j = $rows..$i.
Follow Program 4; continue to Program 6 (Reverse Growing) next.
In short: for each $i from $rows down to 1, print $j from $rows down to $i, then echo PHP_EOL.
Given a positive integer $rows (e.g. 5), print a reverse growing number pattern: row $i prints digits $rows down to $i with no spaces (e.g. when $i = 3 → 543).
// $rows = 5 (conceptual shape)
for ($i = $rows; $i >= 1; $i--) {
for ($j = $rows; $j >= $i; $j--) {
echo $j; // digits $rows..$i
}
echo PHP_EOL; // next row
} | Item | Type | Description |
|---|---|---|
$rows | int | Number of triangle lines — outer loop runs from $rows down to 1. |
$i | int | Outer loop — current row index; sets where the inner loop stops. |
$j | int | Inner loop — descending from $rows down to $i. |
for i from rows down to 1:
for j from rows down to i:
print j
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | 5, 54, 543, … | Learning and interviews |
| User-input rows | fgets(STDIN); | Flexible console programs |
| Spaced output | echo $j . " " | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for ($i = $rows; $i >= 1; $i--) |
Print digits $rows..$i | for ($j = $rows; $j >= $i; $j--) echo $j; |
| End the row | echo PHP_EOL; |
| Spaced digits | echo $j . " "; |
| User input | fgets(STDIN); |
| Program 4 contrast | Program 4 shrinks the prefix (54321, 5432); Program 8 grows it (5, 54, 543) with $j = $rows..$i |
How descending outer $i and inner $j = $rows..$i work together.
for ($i = $rows; $i >= 1; $i--)Lowers stop index $i each row — triangle height.
for ($j = $rows; $j >= $i; $j--)Prints $rows - $i + 1 digits on each row.
echo $jConcatenate digits with no spaces — 123 not 1 2 3.
trace i=3Dry-run when i=3: j=5,4,3 → prints 543.
Reach for this pattern when teaching nested loops, growing inner bounds, and concatenated digit output.
Natural follow-up after Program 7’s growing reverse triangle — each row adds one more digit on the right.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 4 (shrinking rows), then continue to Program 9 (repeated triangle).
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 reverse growing number 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 — inner loop prints $rows..$i on each line.
$rows = 5Hard-coded row count — inner loop prints from $rows down to $i.
<?php
$rows = 5;
for ($i = $rows; $i >= 1; $i--) {
for ($j = $rows; $j >= $i; $j--) {
echo $j;
}
echo PHP_EOL;
} When $i = 3, the inner loop prints 5, 4, 3 — output 543. When $i = 1, output is 54321.
Read the row count with fgets(STDIN) instead of hard-coding 5.
Read $rows with fgets(STDIN); same nested loops as Example 1.
<?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 = $rows; $j >= $i; $j--) {
echo $j;
}
echo PHP_EOL;
} Same nested-loop core as Example 1; only the source of $rows changes.
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 = $rows; $j >= $i; $j--) {
echo $j . " ";
}
echo PHP_EOL;
} Same $j = $rows..$i logic; only the output format adds spaces between digits.
echo is built in; use fgets(STDIN) when reading input. Set loop variables $i, $j with $rows = 5.
for ($i = $rows; $i >= 1; $i--) — each row adds one more digit.
for ($j = $rows; $j >= $i; $j--) — prints digits from $i down to 1.
echo PHP_EOL ends the row after the inner loop finishes.
Each row grows by one digit on the right — O(n²) time, O(1) extra memory.
$i = 3 (when $rows = 5)Trace how lowering $i lengthens each row toward 54321.
Row $i | $j range | Output |
|---|---|---|
| 5 | 5 | 5 |
| 4 | 5, 4 | 54 |
| 3 | 5, 4, 3 | 543 |
| 2 | 5 … 2 | 5432 |
| 1 | 5 … 1 | 54321 |
After the inner loop, echo PHP_EOL moves to the next row.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: compare inner bounds with Program 4 and Program 7 and watch digit order change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 6 for the Reverse Growing pattern.
Practice echo vs echo PHP_EOL without complex math.
Example: put echo PHP_EOL inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use echo $j . " " between digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for $rows = 5 — total is still 15 (1+2+3+4+5)).
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: trace $i and $j on paper for $rows = 3 before coding — watch how each row grows by one digit.
Small habits that keep number-pattern code clean.
Outer loop counts down; inner loop must run $j = $rows..$i so each row grows on the right.
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.
echo $j for Digitsfor ($j = $rows; $j >= $i; $j--) echo $j concatenates digits on one line.
$rows = 3Trace $i = 5, 4, 3 on paper before coding the full $rows = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put echo PHP_EOL inside the inner loop.
Mistakes that commonly break Reverse Growing Number Pattern 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.
for ($i = 1; $i <= $rows; $i++) with $j = $i..1 prints 1, 21, 321 — not 5, 54, 543.
→ Use for ($i = $rows; $i >= 1; $i--) and for ($j = $rows; $j >= $i; $j--).
for ($j = $i; $j >= 1; $j--) builds 1, 21, 321 — not this reverse-growing pattern.
→ Use for ($j = $rows; $j >= $i; $j--) so each row starts at $rows.
Program 4 prints the longest row first (54321). Program 8 prints the shortest row first (5) with the same $j = $rows..$i range.
→ Check output order: growing rows need $i counting down from $rows.
Letters or empty input leave $rows uninitialized.
→ Call is_numeric($input) and re-prompt on failure.
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.
Two rows: 1 and 21.
Unchecked fgets(STDIN) input leaves $rows unset — call is_numeric($input) first.
Each row prints $i digits — total work grows as n(n+1)/2.
Try these variations to lock in the pattern.
54321, 5432…for ($i = $rows; $i >= 1; $i--)5, 54, 543…echo $j . " " between digits$i = $rows..1. Inner loop: $j = $rows..$i with echo $j.echo stays on the line; echo PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 should print a single 1.$i prints $rows - $i + 1 digits ($rows..$i) — Program 4 uses the same inner range but shrinks row by row instead of growing.Quick Takeaway: outer loop $i = $rows..1, inner loop $j = $rows..$i with echo $j, then echo PHP_EOL.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Spaced output (Example 3) | O(n²) | O(1) |
The Reverse Growing Number Pattern is a compact nested-loop lesson: outer loop lowers $i while the inner loop prints digits $rows down to $i. Master the fixed-$rows version, then try user input and spaced output.
Practice the three examples above, then continue to Program 9 for the repeated number triangle (1, 22, 333, 4444, 55555).
Each row prints $rows..$i — keep echo $j for digits and echo PHP_EOL for the row break.
for ($i = $rows; $i >= 1; $i--) in the outer loopfor ($j = $rows; $j >= $i; $j--) prints digits i..1echo $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 loopj-- from $iecho PHP_EOL inside the inner loop — digits must stay on one row$rows = 1 edge casePrint the pattern the beginner-friendly way.
Row prints $rows..$i
Counts down $i
$j = $rows down to $i
Ends each row
ShapeO(n²) time
AnalysisOuter loop counts $i down from $rows; inner loop prints $j from $rows down to $i — 5, 54, 543, … O(n²) for $n rows.
Move on to the repeated number triangle pattern in the PHP number-pattern series.
12 people found this page helpful