PHP Sequential Number Triangle Pattern (Narrowing)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A decreasing continuous number triangle prints values from a shared counter $k that never resets, while each row prints one fewer number than the row above.
Remember
Rule: row i prints (rows - i + 1) values of k++
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15 ← 5 rows
In PHP the inner loop runs from $rows down to $i, so the first row is the longest. Use printf("%3d", $k++) to keep columns aligned past single digits.
Approach
How to Solve It
One nested-loop idea with a shared counter and format width.
Method
Idea
Best for
Counter + shrink
Print $k++ while row length shrinks
Learning, interviews, exams
User-input rows
Same logic with a variable height
Practice / demos
Pseudocode
Pseudocode
k = 1
for i from 1 to rows:
for j from rows down to i:
print k (width 3), then k = k + 1
print newline
Cheat sheet
Goal
Pattern
Shrink each row
for ($i = 1; $i <= $rows; $i++)
Row length
for ($j = $rows; $j >= $i; $j--)
Next number
printf("%3d", $k++);
End the row
echo PHP_EOL;
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%3d", $k++)
Stays on the same line
Each formatted number
echo PHP_EOL
Ends the current line
After the inner loop
Print numbers without a newline, then end the row once.
Try it
Live Preview
Change the row count and the shrinking continuous triangle updates instantly.
Whole numbers from 1 to 8 (width-3 columns stay readable). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 values
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
Trace
Worked Walkthrough — $rows = 4
Trace how many values each row takes from counter $k.
$i
Count / values
Printed row
1
4 / 1..4
1 2 3 4
2
3 / 5..7
5 6 7
3
2 / 8 9
8 9
4
1 / 10
10
Row length is $rows - $i + 1. Counter $k ends at n(n+1)/2.
Code
PHP Programs
Three complete programs: fixed height, CLI input, and a compact 3-row demo. Use View Output for sample results.
Example 1 — Fixed $rows = 5
Shrinking inner loop prints continuous $k++ values with width-3 formatting.
Inner bound:for ($j = $rows; $j >= $i; $j--) shrinks the row each time.
printf vs PHP_EOL: numbers stay on the line; echo PHP_EOL advances after each row.
Next step: Program 39 prints a rotating number pattern (12345, 23451, …).
One line: for each row, print the next values from a shared counter while the row length shrinks by one.
Frequently Asked Questions
Continuous numbers starting from 1, but each row has one fewer number: 5 on row 1, then 4, 3, 2, and 1 — totaling 15 numbers for 5 rows.
Counter $k starts at 1 before the loops and increments with $k++ each time a number prints — it is never reset inside the outer loop.
Row i prints rows - i + 1 numbers — the inner loop runs from $j = $rows down to $i.
The format specifier reserves 3 columns per number, keeping columns aligned when values become two digits.
Program 35 is right-aligned with leading spaces. Program 38 is left-aligned with decreasing row width and the same continuous counter.
Replace 5 with $rows in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
Only one row prints — a single number 1.
🤔
Did you know?
A counter $k starts at 1 and increments every time a number prints. Row $i prints $rows - $i + 1 numbers with printf("%3d", $k++) — total prints = n(n+1)/2.