An ascending number triangle grows one digit per row: row $i prints 1 through $i with no spaces between digits. With $rows = 5 you get 1, 12, 123, 1234, 12345.
Remember
Rule: on row i, print j for j = 1..i
1
12
123
1234
12345 ← rows = 5
Unlike Program 4 (descending $rows..$i shrink), this shape counts the outer loop up so each row is longer than the last.
Approach
How to Solve It
Outer loop picks row $i from 1 to $rows. Inner loop prints digits 1..$i.
Method
Idea
Best for
Tight digits
echo $j with no spaces
Classic textbook demos
Spaced digits
echo $j . " "
Easier reading when digits get wider
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print j (no newline)
print newline
Cheat sheet
Goal
Pattern
Outer rows
for ($i = 1; $i <= $rows; $i++)
Inner digits
for ($j = 1; $j <= $i; $j++)
Print digit
echo $j;
Spaced form
echo $j . " ";
End the row
echo PHP_EOL;
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j
Stays on the same line
Each digit
echo PHP_EOL
Ends the current line
After the inner loop
Print digits without a newline, then end the row once. Putting echo PHP_EOL inside the inner loop turns the triangle into a vertical list.
Try it
Live Preview
Change the row count and the ascending triangle updates instantly.
Use rows from 1 to 9 (single digits stay tidy). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
1
12
123
1234
12345
Trace
Worked Walkthrough
Trace row $i = 4 — four digits glued together as 1234.
$j
Print
Line so far
1
1
1
2
2
12
3
3
123
4
4
1234
After $j finishes, echo PHP_EOL starts the next row. Row 5 will print one more digit — 12345.
Code
PHP Programs
Three complete programs: fixed $rows = 5, CLI input, and spaced digits. Use View Output to reveal sample results.
Example 1 — Fixed $rows = 5
Hard-coded height — outer loop grows the row, inner loop prints 1..$i.
1. Only the print changes.echo $j . " " instead of echo $j.
2. Loop bounds stay the same. Outer and inner limits match Example 1 exactly.
3. Useful for wider values. Spaces keep columns readable when digits become two characters.
Edge Cases & Pitfalls
Check these before calling the solution done.
Newline
Do not put PHP_EOL inside the inner loop
That prints one digit per line. Call echo PHP_EOL only after the inner loop.
Outer direction
Count up, not down
Outer $i = $rows..1 with the same inner loop is Program 1 (shrinking). This pattern uses $i = 1..$rows.
Missing newline
Omitting the row break
Without echo PHP_EOL, every digit glues onto one endless line.
j > i
Wrong inner bound
Using $j <= $rows makes every row the same length. Keep $j <= $i.
rows = 1
Single value
Output is just 1. A good sanity check for input validation.
fgets
Check is_numeric
Bare (int) casts turn non-numeric strings into 0 — prefer is_numeric before casting.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–3)
O(n²)
O(1)
Total prints are 1 + 2 + … + n = n(n + 1) / 2 — quadratic in n. Only a few integers of extra memory.
Remember
Key Takeaways
Rule: on row $i, print $j for $j = 1..$i.
Grow up: outer $i = 1..$rows makes each row one digit longer.
Break the row: call echo PHP_EOL only after the inner loop.
Complexity:O(n²) time; O(1) extra space.
One line: for each row $i from 1 to $rows, print digits 1..$i, then end the line.
Frequently Asked Questions
The outer loop runs $i from 1 to $rows. For each row $i, the inner loop runs $j from 1 to $i and prints $j. Row 1 prints 1, row 2 prints 12, and so on until row $rows prints 1..$rows.
Because the outer loop counts up and the inner bound equals $i. When $i increases, each row prints one more digit than the previous row.
Program 4 prints $rows..$i in descending order (54321, 5432, …). Program 5 prints 1..$i in ascending order — a growing triangle instead of a shrinking one.
Program 1 counts the outer loop down and prints 12345, 1234, … Program 5 counts up and prints 1, 12, 123, … — same inner loop, opposite outer direction.
Printing a digit stays on the same line. Printing PHP_EOL ends the current row. Digits use echo without a newline; the row break uses echo PHP_EOL after the inner loop.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.
Use echo $j . " " instead of echo $j — see Example 3.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
🤔
Did you know?
Row $i prints digits 1 through $i. The outer loop counts up from 1 to $rows, so each row grows by one digit — total prints = n(n+1)/2, time O(n²).