Shape Rule
$rows..$i per row
Row 1 prints 54321, row 2 prints 5432, shrinking the tail while the first digit stays $rows.

The left-aligned descending number triangle prints 54321, 5432, 543, 54, 5 — a natural step after Program 3 where the first digit changes each row. This tutorial covers ascending outer and descending inner loops, a live preview, algorithm steps, worked PHP examples, edge cases, and complexity.
$rows..$i per row
Row 1 prints 54321, row 2 prints 5432, shrinking the tail while the first digit stays $rows.
1..rows
for ($i = 1; $i <= $rows; $i++) moves the inner-loop stop forward each row.
$rows..$i descending
for ($j = $rows; $j >= $i; $j--) always starts at $rows and counts down to $i.
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 left-aligned descending triangle in the browser.
Complexity
Total digit prints = n(n+1)/2; extra memory stays O(1).
A left-aligned descending number triangle keeps the same starting digit on every row while the tail gets shorter. With $rows = 5, the output is 54321, 5432, 543, 54, 5.
In PHP the outer loop runs $i = 1..$rows, the inner loop prints $j from $rows down to $i, then echo PHP_EOL moves to the next line.
It teaches how changing the inner-loop start (always $rows) creates a fixed-prefix triangle — compare with Program 3 next.
Every row starts at $rows.
Inner loop $j = $rows..$i shortens each row.
Program 3 changes the first digit; Program 4 keeps it at $rows.
Follow Program 3; continue to Program 5 (ascending triangle) next.
In short: for each $i from 1 to $rows, print $j from $rows down to $i, then echo PHP_EOL.
Given a positive integer $rows (e.g. 5), print a left-aligned descending triangle: each row prints digits from $rows down to $i, with the outer loop counting from 1 up to $rows.
// $rows = 5
//54321
//5432
//543
//54
//5 | Item | Type | Description |
|---|---|---|
$rows | int | Number of triangle lines — outer loop runs from 1 up to $rows. |
$i | int | Outer loop — current row index; sets where the inner loop stops. |
$j | int | Inner loop — descending from $rows down to $i; always starts at the max digit. |
for i from 1 to rows:
for j from rows down to i:
print j
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | 54321, 5432, … | Learning and interviews |
| User-input rows | trim(fgets(STDIN)); | Flexible console programs |
| Spaced output | echo $j . " " | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for ($i = 1; $i <= $rows; $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 | trim(fgets(STDIN)); |
| Program 3 contrast | for ($i = $rows; $i >= 1; $i--) with $j = $i..1 |
Same triangle — how the two loop directions work together.
$i = 1..$rowsMoves the inner stop forward — shortens each row
$j = $rows..$iAlways starts at max digit, counts down to $i
starts at $rowsEvery row begins with the same first digit
compare P3Swap inner start from $i to $rows to get this shape
Reach for this pattern when teaching descending inner loops and shrinking row lengths.
Natural follow-up after Program 3 — fixed first digit with a shrinking tail.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 3 (left-shifted), then continue to Program 5 (ascending).
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 left-aligned descending triangle in the browser.
Three complete PHP programs — fixed rows, user input, and spaced output variant. Click View Output to reveal sample console results.
Print five rows with ascending outer and descending inner loops.
$rows = 5Hard-coded row count — inner loop always starts at $rows.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $rows; $j >= $i; $j--) {
echo $j;
}
echo PHP_EOL;
} When $i = 1, the inner loop prints 5, 4, 3, 2, 1 — output 54321. When $i = 5, only 5 prints. The outer loop increases $i to shorten each row.
Read the row count with fgets(STDIN) instead of hard-coding 5.
Read $rows with trim(fgets(STDIN)); both loops use the input as the max digit.
<?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 = 1; $i <= $rows; $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. Non-numeric input fails is_numeric() — validate before casting to int for safer labs.
Add a space between digits for easier reading on each row.
Append a space after each digit for easier reading.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $rows; $j >= $i; $j--) {
echo $j . " ";
}
echo PHP_EOL;
} Same loop structure; only echo $j . " " adds spacing between digits.
Set $rows = 5; and use fgets(STDIN) when reading input. Set loop variables $i, $j.
for ($i = 1; $i <= $rows; $i++) — ascending outer loop moves the inner stop forward each row.
for ($j = $rows; $j >= $i; $j--) — prints digits from $rows down to $i.
echo PHP_EOL ends the row after the inner loop finishes.
Tail shortens while the first digit stays at $rows — O(n²) time, O(1) extra memory.
$rows = 5Trace each outer-loop value of $i, the inner-loop range, digit count, and full row output.
$i | Inner loop ($j) | Prints | Row output |
|---|---|---|---|
1 | 5, 4, 3, 2, 1 | 5 | 54321 |
2 | 5, 4, 3, 2 | 5 | 5432 |
3 | 5, 4, 3 | 5 | 543 |
4 | 5, 4 | 5 | 54 |
5 | 5 | 5 | 5 |
Prints per row = rows - i + 1 — total prints = n(n+1)/2 for n rows.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: flip j-- to j++ and watch digit order change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 5 for an ascending number triangle.
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 15 (5+4+3+2+1).
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 shortens by one digit.
Small habits that keep number-pattern code clean.
Outer loop counts down ($i--); inner loop must also count down from $i to 1.
fgets(STDIN)Call is_numeric($input) so bad input does not leave $rows uninitialized.
Only call echo PHP_EOL after the inner loop finishes the row.
for ($j = $rows; $j >= $i; $j--) prints digits $rows..$i in reverse order.
$rows = 3Trace $i = 1, 2, 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 left-aligned descending number triangles.
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 (j = 1; j <= i; j++) prints ascending digits — you get Program 1’s shape, not this one.
→ Keep for ($j = $rows; $j >= $i; $j--) so each row reads $rows..$i.
for ($i = $rows; $i >= 1; $i--) changes the first digit each row — you get Program 3’s shape.
→ Use for ($i = 1; $i <= $rows; $i++) so every row starts at $rows.
j = i on every row prints a left-shifted tail — that is Program 3, not this pattern.
→ Start the inner loop at the fixed top value: j = 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: 21 and 2.
Unchecked fgets(STDIN) input leaves $rows unset — call is_numeric($input) first.
Each row prints $rows - $i + 1 digits — total work grows as n(n+1)/2.
Try these variations to lock in the pattern.
i..11..iecho $j . " " between digits$i = 1..$rows. Inner loop: $j = $rows..$i with $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 exactly $rows - $i + 1 digits — compare with Program 3 where each row prints $i digits.Quick Takeaway: outer loop $i = 1..$rows, 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) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The left-aligned descending number triangle is a compact nested-loop lesson: an ascending outer loop moves the inner stop forward while the inner loop prints digits from $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 5 for the ascending number triangle.
Row $i prints $rows..$i — keep echo $j for digits and echo PHP_EOL for the break, and validate row counts when reading input.
for ($i = 1; $i <= $rows; $i++) in the outer loopfor ($j = $rows; $j >= $i; $j--) prints digits in reverseecho $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$rows = 1 edge casePrint the pattern the beginner-friendly way.
Row $i prints $rows..$i
Counts up rows
Codej = rows down to i
CodeEnds each row
ShapeO(n²) time
AnalysisThe inner loop always starts at $rows and counts down to $i, so every row begins with the same digit while the tail shortens — 54321, 5432, 543, and so on.
Move on to the ascending number triangle in the PHP number-pattern series.
12 people found this page helpful