A continuous number triangle grows one number longer each row, but the counter never resets — values keep climbing across the whole pattern.
Remember
Rule: k = 1; for i = 1..n, print k++ (i times)
1
2 3
4 5 6
7 8 9 10 ← 4 rows
In PHP declare $k = 1 once, print $k++ in the inner loop for $i iterations per row, then echo PHP_EOL ends the row. Program 21 jumps ahead with a different step rule.
Approach
How to Solve It
Declare $k = 1 before both loops. Outer loop from 1 to $rows. Inner loop prints $k++ exactly $i times. End each row with echo PHP_EOL.
Method
Idea
Best for
Running counter
$k outside loops; print $k++ each cell
Learning, interviews, exams
Custom start
Set $k to any start value before the loops
When the sequence should not begin at 1
Pseudocode
Pseudocode
k = 1
for i from 1 to rows:
for j from 1 to i:
print k and a space
k = k + 1
print newline
Cheat sheet
Goal
Pattern
Init counter
$k = 1; before both loops
Pick row length
for ($i = 1; $i <= $rows; $i++)
Print & advance
echo $k++ . " ";
End the row
echo PHP_EOL;
Custom start
$k = 10; (or any value) before the loops
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $k++ . " "
Stays on the same line
Each number in the row
echo PHP_EOL
Ends the current line
After the inner loop
Print all numbers without a newline, then end the row once.
Try it
Live Preview
Change the row count and the continuous triangle updates instantly.
Whole numbers from 1 to 12. Tap a chip or type a value — the preview redraws as you go.
Live result4 rows · 10 numbers
1
2 3
4 5 6
7 8 9 10
Trace
Worked Walkthrough
Trace $rows = 4 with $k starting at 1 — watch the counter carry into the next row.
$i
Prints
$k after row
Printed row
1
1×
2
1
2
2×
4
2 3
3
3×
7
4 5 6
4
4×
11
7 8 9 10
Total numbers are 1+2+3+4 = 10 — the triangular number n(n+1)/2.
Code
PHP Programs
Three complete programs: fixed height, CLI input, and a custom start for $k. Use View Output for sample results.
Example 1 — Fixed $rows = 4
Declare $k once; print and advance it in the inner loop.
1. Same structure. Outer and inner loops match Example 1.
2. Different seed.$k = 10 only changes where the sequence begins.
3. Same length rule. Rows still print 1, 2, 3, then 4 numbers — only the values shift.
Edge Cases & Pitfalls
Check these before calling the solution done.
reset k
$k = 1 inside the outer loop
That restarts every row at 1. Keep $k outside both loops for a continuous sequence.
PHP_EOL inside
echo PHP_EOL inside the inner loop
That puts each number on its own line. Call echo PHP_EOL only after the row finishes.
rows = 1
Single number
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
multi-digit
Large row counts
After 9 the counter becomes two digits. Spacing still works; alignment just looks wider.
spaces
Keep the trailing space
The classic sample uses echo $k++ . " " so numbers stay spaced and readable.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input / custom start (Examples 1–3)
O(n²)
O(1)
There are n rows printing 1+2+…+n numbers, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: init $k = 1 once; for each row $i, print $k++ exactly $i times.
Scope: keep $k outside the outer loop so the sequence never restarts.
echo vs PHP_EOL: numbers stay on the line; echo PHP_EOL advances after each row.
Next step: Program 21 jumps ahead with a different increment rule.
One line: keep $k outside, print $k++$i times per row, then echo PHP_EOL.
Frequently Asked Questions
Because $k is declared once ($k = 1) before the loops and incremented with $k++ every time a number is printed — its value carries over to the next row.
Row 1 prints 1, so $k becomes 2. Row 2 prints $k twice: 2, then 3 after $k++.
$k resets each row and the sequence restarts at 1 — you get a growing triangle, not a continuous counter.
echo $k++ . " " stays on the same line with a trailing space. echo PHP_EOL ends the current line. Numbers use echo; the row break uses echo PHP_EOL after the inner loop.
Total prints are 1+2+...+n = n(n+1)/2.
Yes. Set $k = 10 (or any value) before the loops — the sequence continues from that start (see Example 3).
O(n²) for n rows because total digit prints equal n(n+1)/2.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int so bad input does not produce unexpected results.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
A single counter k starts at 1 and increments with k++ on every print — numbers continue across rows instead of restarting. Total prints still equal n(n+1)/2 for n rows.