Shape Rule
Print 1..$i
Row $i prints digits 1, 2, …, $i with no spaces.

The ascending number triangle prints 1, 12, 123, 1234, 12345 — row $i concatenates digits 1 through $i. This tutorial covers nested-loop logic, live preview, worked PHP examples, edge cases, and O(n²) complexity.
Print 1..$i
Row $i prints digits 1, 2, …, $i with no spaces.
i = 1..rows
Outer loop picks row $i; inner loop runs $j = 1..$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 ascending triangle instantly in the browser.
Complexity
Total digits ≈ n(n+1)/2 — quadratic time; extra memory stays O(1).
An ascending number triangle pattern grows each row by one digit: 1, 12, 123, up to 12345 for five rows. Row $i always starts at 1 and runs through $i.
In PHP the outer loop runs $i = 1..$rows, the inner loop echoes $j from 1 up to $i, then echo PHP_EOL moves to the next line.
It is a foundational nested-loop exercise — compare with Program 4 (fixed prefix descending) and continue to Program 6 (increasing suffix).
Every row begins at digit 1.
Inner loop $j = 1..$i lengthens each row.
Program 4 keeps the first digit at $rows; Program 5 grows from 1 to i.
Follow Program 4; continue to Program 6 (increasing suffix) next.
In short: for each $i from 1 to $rows, echo $j from 1 up to $i, then echo PHP_EOL.
Given a positive integer $rows (e.g. 5), print an ascending number triangle: row $i prints digits 1 through $i with no spaces.
// $rows = 5 (conceptual shape)
// 1
// 12
// 123
// 1234
// 12345 | 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 — ascending from 1 up to i. |
for $i from 1 to $rows:
for $j from 1 up to $i:
echo $j
newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | 1, 12, 123, … | Learning and interviews |
| User-input rows | (int) $input after is_numeric() | Flexible console programs |
| Spaced output | echo $j . " " | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for ($i = 1; $i <= $rows; $i++) |
| Print digits 1..$i | for ($j = 1; $j <= $i; $j++) echo $j; |
| End the row | echo PHP_EOL; |
| Spaced digits | echo $j . " "; |
| User input | (int) $input after is_numeric() |
| Program 4 contrast | Program 4 uses $j = $rows..$i (fixed prefix); Program 5 uses $j = 1..$i |
How outer row selection and inner $j=1..$i work together.
for ($i = 1; $i <= $rows; $i++)Picks row number i — triangle height.
for ($j = 1; $j <= $i; $j++)Prints exactly $i digits on row $i.
echo $jConcatenate digits with no spaces — 123 not 1 2 3.
trace $i=3Dry-run row 3: $j=1..3 → prints 123.
Reach for this pattern when teaching nested loops, growing inner bounds, and concatenated digit output.
Natural follow-up after left-aligned descending triangles — now the prefix grows from 1.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 4 (descending prefix), then continue to Program 6 (increasing suffix).
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 ascending number triangle 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 1..$i on each line.
$rows = 5Hard-coded row count — inner loop prints from 1 to $i.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo $j;
}
echo PHP_EOL;
} When $i = 3, the inner loop prints 1, 2, 3 — output 123. When $i = 5, output is 12345.
Read the row count with fgets(STDIN) instead of hard-coding 5.
Read $rows with fgets(STDIN) with is_numeric(); 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 = 1; $i <= $rows; $i++) {
for ($j = 1; $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 = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo $j . " ";
}
echo PHP_EOL;
} Same $j = 1..$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 = 1; $i <= $rows; $i++) — each row adds one more digit.
for ($j = 1; $j <= $i; $j++) — echoes digits from 1 up to $i.
echo PHP_EOL ends the row after the inner loop finishes.
Each row grows by one digit — O(n²) time, O(1) extra memory.
$i = 3Trace row 3 to see how the inner loop builds 123.
| j | Action | Row so far |
|---|---|---|
| 1 | print 1 | 1 |
| 2 | print 2 | 12 |
| 3 | print 3 | 123 |
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: flip j-- to j++ and watch digit order change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 6 for the increasing suffix pattern.
Practice echo.print 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 grows by one digit.
Small habits that keep number-pattern code clean.
Outer loop counts up; inner loop must run $j = 1..$i for the ascending prefix.
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 = 1; $j <= $i; $j++) echo $j; concatenates digits on one line.
Trace $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 ascending number triangle 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.
Every row prints $rows digits — you get a rectangle, not a growing triangle.
→ Keep for ($j = 1; $j <= $i; $j++) so row length equals $i.
for ($j = $i; $j >= 1; $j--) reverses digit order — still a triangle, but not 1, 12, 123.
→ Use ascending inner loop $j = 1..$i for the standard pattern.
j = rows..i produces Program 4’s fixed-prefix shape — not this pattern.
→ Start the inner loop at $j = 1 for ascending 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 12.
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, 45, 345…echo $j . " " between digits$i = 1..$rows. Inner loop: $j = 1..$i with echo $j.echo.print 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 $i digits — compare with Program 4 where each row prints $rows - $i + 1 digits.Quick Takeaway: outer loop $i = 1..$rows, inner loop $j = 1..$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 ascending number triangle is a compact nested-loop lesson: outer loop grows rows while the inner loop echoes digits 1 through $i. Master the fixed-$rows version, then try user input and spaced output.
Practice the three examples above, then continue to Program 6 for the increasing suffix pattern.
Row $i prints 1..$i — keep echo $j for digits and echo PHP_EOL for the row break.
for ($i = 1; $i <= $rows; $i++) in the outer loopfor ($j = 1; $j <= $i; $j++) echoes digits 1..$iecho $j for digits and echo PHP_EOL after each rowrows ≥ 1 for interactive programsis_numeric($input) before using $rowsecho PHP_EOL inside the inner digit loopj <= rows when triangle shape needs $j <= $i$rows (that is Program 4, not this pattern)$rows = 1 edge casePrint the pattern the beginner-friendly way.
Row $i prints 1..$i
DefinitionCounts up rows
Code$j = 1 to $i
CodeEnds each row
ShapeO(n²) time
AnalysisThe inner loop always runs from $j = 1 to $j = $i, so row $i prints digits 1 through $i — 1, 12, 123, and so on.
Move on to the increasing suffix number pattern in the PHP number-pattern series.
12 people found this page helpful