PHP Number Triangle Pattern (Consecutive Offset)

Definition
What Is This Pattern?
An increasing number triangle from 1 prints $i values on row $i, each computed as $i + $j - 1 — so each row starts with its own row number.
Rule: for i from 1 to rows
for j from 1 to i
print (i + j - 1) and a space
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9 ← rows = 5
Follows the triangle from 11 in Program 32; next is the variant from 0 in Program 34.
Approach
How to Solve It
Outer loop grows $i. Inner loop prints $i values with the formula $i + $j - 1, separated by spaces.
| Method | Idea | Best for |
|---|
Formula $i + $j - 1 | When $j = 1, value equals $i — row starts at its index | Learning, interviews, exams |
| Compare offsets | Same shape as Program 32, but no base-9 shift | Seeing how formulas change the start |
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print (i + j - 1) and a space
print newline
Cheat sheet
| Goal | Pattern |
|---|
| Grow each row | for ($i = 1; $i <= $rows; $i++) |
| Print i values | for ($j = 1; $j <= $i; $j++) |
| Start at 1 | echo ($i + $j - 1) . " "; |
| Row starts at i | When $j = 1, $i + $j - 1 = $i |
| End of row | echo PHP_EOL; |
Printing Numbers vs Starting a New Line
| API | Effect | Use for |
|---|
echo value . " " | Stays on the same line | Each number on the row |
echo PHP_EOL | Ends the current line | After the inner loop finishes |
Print values without a newline, then end the row once.
Try it
Live Preview
Change the row count and the increasing triangle updates instantly — capped at 9 for readable demos.
Trace
Worked Walkthrough — $rows = 5
Trace how each ($i, $j) pair maps to $i + $j - 1.
$i | Values | Count | Prints |
|---|
1 | 1 | 1 | 1 |
2 | 2, 3 | 2 | 2 3 |
3 | 3, 4, 5 | 3 | 3 4 5 |
4 | 4..7 | 4 | 4 5 6 7 |
5 | 5..9 | 5 | 5 6 7 8 9 |
Total numbers = 1 + 2 + … + n = n(n+1)/2 → O(n²).
Code
PHP Programs
Three complete programs: fixed $rows = 5, CLI input, and a compact $rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed $rows = 5
Hard-coded height — print $i + $j - 1 with a trailing space on each cell.
<?php
for ($i = 1; $i <= 5; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo ($i + $j - 1) . " ";
}
echo PHP_EOL;
}
?>
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
How It Works
1. Outer grows the width. Row $i prints exactly $i numbers.
2. Formula fills the cells. $i + $j - 1 makes row $i start at $i and count upward.
3. Newline once. Call echo PHP_EOL only after the inner loop finishes.
Example 2 — User Input Rows
Read $rows from the CLI, validate, then use the same formula core.
<?php
echo "Enter rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input <= 0) {
echo "Please enter a positive integer." . PHP_EOL;
exit(1);
}
$rows = (int) $input;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo ($i + $j - 1) . " ";
}
echo PHP_EOL;
}
?>
How It Works
1. Prompt and validate. Reject non-numeric and non-positive values before printing.
2. Same core. $i + $j - 1 matches Example 1 — only $rows comes from the user.
3. Safer input tip. Cap demos for readable output:
if (!is_numeric($input) || (int) $input < 1 || (int) $input > 9) {
echo "Enter a whole number from 1 to 9." . PHP_EOL;
exit(1);
}
Example 3 — Compact $rows = 3
Same structure with only three rows — easy to confirm the formula on paper.
<?php
$rows = 3;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo ($i + $j - 1) . " ";
}
echo PHP_EOL;
}
?>
How It Works
1. Three rows. $i = 1 → 1; $i = 2 → 2 3; $i = 3 → 3 4 5.
2. Trace on paper. If you use $i + $j instead of $i + $j - 1, every value shifts up by one.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
i + jMissing −1
Using $i + $j or 9 + $i + $j shifts every value — rows no longer start at the row number. Keep $i + $j - 1.
j <= rowsRectangle instead of triangle
Inner bound $j <= $rows makes every row the same width. Use $j <= $i.
no spaceGlued numbers
Without . " ", values run together (23). Always print a trailing space.
PHP_EOL insideBroken rows
If echo PHP_EOL sits inside the inner loop, you get one number per line. Call it only after the loop.
rows = 1Single value
Output is just 1. A good sanity check for input validation.
fgetsCheck 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 |
|---|
| Fixed / input (Examples 1–2) | O(n²) | O(1) |
| Compact rows = 3 (Example 3) | O(n²) | O(1) |
Row $i prints i numbers. Total = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Formula: each cell is $i + $j - 1 — row $i starts at $i.
Growing width: inner loop runs $j = 1..$i so row $i has $i numbers.
Break the row: values with spaces in the inner loop; echo PHP_EOL after it finishes.
Complexity: O(n²) time from n(n+1)/2 prints; O(1) extra space.
One line: for each row $i, print $i + $j - 1 for $j = 1..$i with spaces, then echo PHP_EOL.
Frequently Asked Questions
An increasing triangle from 1: for rows=5 you get 1 / 2 3 / 3 4 5 / 4 5 6 7 / 5 6 7 8 9 — each value is $i + $j - 1.
Because when $j = 1, the expression $i + $j - 1 becomes $i. Row 4 therefore starts with 4.
$j increases by 1, so $i + $j - 1 increases by 1 as well — producing consecutive numbers on each row.
Program 32 uses 9 + $i + $j (starts at 11). Program 33 uses $i + $j - 1 (starts at 1).
Program 33 uses $i + $j - 1 with $i starting at 1. Program 34 uses $i + $j with $i starting at 0.
echo ($i + $j - 1) . " " keeps values separated on the same row. echo PHP_EOL ends the row.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.
🤔
Did you know?
Each printed value is computed as $i + $j - 1. Row $i = 1 prints 1; row $i = 4 prints 4, 5, 6, 7 — a left-shifted increasing triangle starting at 1.
Next: Increasing Triangle from 0
Continue with the same shape using $i + $j and an outer loop from 0.
Program 34 tutorial →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful