Shape Rule
Left-shifted triangle
Row $i prints $i numbers computed as $i + $j - 1.
$i + $j - 1 in PHP
The increasing number triangle using $i + $j - 1 prints 1, 2 3, 3 4 5, … — a natural follow-up after Program 32’s triangle starting from 11. This tutorial covers the $i + $j - 1 formula, nested loops, a live preview, worked PHP examples, edge cases, and complexity.
Left-shifted triangle
Row $i prints $i numbers computed as $i + $j - 1.
$i = 1..$rows
for ($i = 1; $i <= $rows; $i++) — one growing row per iteration.
1..$i
for ($j = 1; $j <= $i; $j++) — prints $i values per row.
$i + $j - 1
Each value is $i + $j - 1 — row $i starts at $i when $j = 1.
3–9 rows
Pick a row count and draw the increasing triangle in the browser.
Complexity
Prints per row = $i — total work scales as n².
A left-shifted increasing number triangle prints values from the formula $i + $j - 1 on each row. With $rows = 5, you get 1, 2 3, 3 4 5, and so on.
In PHP you use nested loops: outer $i = 1..$rows, inner $j = 1..$i, printing ($i + $j - 1) with a trailing space.
It combines nested loops with a compact arithmetic formula — a step after Program 32’s 9 + $i + $j offset.
Formula for each value.
Growing row width.
When i=1, j=1 → 1.
Follow Program 32; continue to Program 34 ($i + $j from 0) next.
In short: outer loop $i = 1..$rows, inner $j = 1..$i, print $i + $j - 1 with a space, then echo PHP_EOL.
Given $rows = 5, print a left-shifted increasing triangle: for each row $i, print $j = 1..$i values of $i + $j - 1 separated by spaces.
// $rows = 5 (conceptual shape)
// 1
// 2 3
// 3 4 5
// 4 5 6 7
// 5 6 7 8 9 | Item | Type | Description |
|---|---|---|
$rows | int | Triangle height — number of lines to print. |
$i | int | Outer loop — current row; also part of the formula. |
$j | int | Inner loop — column index; runs 1..$i per row. |
for i from 1 to rows:
for j from 1 to i:
print ($i + $j - 1) + space
print newline | Approach | Idea | Best for |
|---|---|---|
| Fixed formula | 1, 2 3, … | Learning and interviews |
| User-input rows | (int) trim(fgets(STDIN)); | Configurable triangle size |
| Compact trace | $rows = 3 on paper first | Debugging loop bounds |
| Goal | Pattern |
|---|---|
| Outer loop | for ($i = 1; $i <= $rows; $i++) |
| Inner loop | for ($j = 1; $j <= $i; $j++) |
| Print value | echo ($i + $j - 1) . " "; |
| End the row | echo PHP_EOL; |
| User input | (int) trim(fgets(STDIN)); |
Same increasing triangle — different ways to control the row count.
$i = 1..$rowsOne growing row per iteration
$i + $j - 1Starts at 1
$j = 1..$ii values per row
$j = 1 → $iRow starts at row number
Reach for this pattern when teaching formula-based output, growing inner loops, and arithmetic in nested loops.
Natural follow-up after the triangle starting from 11 — uses $i + $j - 1 to start at 1.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare Program 32 (9 + $i + $j) and Program 34 ($i + $j from 0) next.
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.
Choose a row count between 3 and 9 and draw the increasing triangle in the browser.
Three complete PHP programs — fixed rows, user input, and a smaller trace demo. Click View Output to reveal sample console results.
Print five rows of the increasing triangle with the $i + $j - 1 formula.
$rows = 5Hard-coded row count — ideal for first demos and screenshots.
<?php
for ($i = 1; $i <= 5; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo ($i + $j - 1) . " ";
}
echo PHP_EOL;
} When $i = 1, the inner loop prints 1+1-1 = 1. When $i = 4, it prints 4, 5, 6, 7 — output 4 5 6 7.
Read the row count with fgets(STDIN) instead of hard-coding 5.
Read $rows with (int) trim(fgets(STDIN)) instead of hard-coding 5.
<?php
echo "Enter rows: ";
$rows = (int) trim(fgets(STDIN));
if ($rows < 1) return;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo ($i + $j - 1) . " ";
}
echo PHP_EOL;
} Same formula core as Example 1; only $rows comes from user input instead of being hard-coded as 5. Non-numeric input leaves $rows unset if you skip is_numeric() checks — always check it in safer labs.
Run with $rows = 3 to trace every row on paper before scaling up.
$rows = 3Same nested-loop formula with a smaller row count for quick tracing.
<?php
$rows = 3;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo ($i + $j - 1) . " ";
}
echo PHP_EOL;
} Only $rows changes from 5 to 3 — the nested-loop formula stays identical. Trace $i = 1, 2, 3 on paper to see how each row adds one more value.
Set $rows = 5; and use fgets(STDIN) when reading input. Set loop variables $i, $j.
for ($i = 1; $i <= $rows; $i++) — one growing row per iteration.
for ($j = 1; $j <= $i; $j++) — prints $i values per row.
echo ($i + $j - 1) . " " — each value from the arithmetic formula.
echo PHP_EOL ends the row after the inner loop finishes.
Prints per row = $i — O(n²) time, O(1) extra memory.
$rows = 5Trace each outer-loop value of $i, inner-loop range, values printed, and full row output.
$i | Inner range ($j) | Values (i+j-1) | Row output |
|---|---|---|---|
1 | 1 | 1 | 1 |
2 | 1, 2 | 2, 3 | 2 3 |
3 | 1, 2, 3 | 3, 4, 5 | 3 4 5 |
4 | 1..4 | 4, 5, 6, 7 | 4 5 6 7 |
5 | 1..5 | 5, 6, 7, 8, 9 | 5 6 7 8 9 |
Prints per row = $i — total prints = n(n+1)/2 for n rows.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change inner bound to $j <= $rows and watch every row print the same width.
Foundation for formula-based triangles and left-shifted sequences starting at 1.
Example: continue to Program 34 for the $i + $j variant starting from 0.
Practice echo 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 for wider spacing.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for $rows = 5 — total is 1+2+3+4+5 = 15.
Pair the pattern with is_numeric(trim($line)) checks and positive-row checks.
Example: reject max <= 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 row $i starts at $i when $j = 1.
Small habits that keep number-pattern code clean.
Inner bound must be $j <= $i — row $i prints exactly $i numbers.
trim(fgets(STDIN))Call is_numeric(trim($line)) so bad input does not leave $rows uninitialized.
Only call echo PHP_EOL after the inner loop finishes the row.
Trace the formula for each ($i, $j) pair before coding the loops.
Trace $i = 1..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 increasing number triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use echo ($i + $j - 1) . " "; echo PHP_EOL only after the inner loop.
Using $i + $j or 9 + $i + $j shifts every value — rows no longer start at the row number.
→ Keep $i + $j - 1 so row $i begins at $i.
$j <= $rows prints a rectangle — every row has the same width.
→ Keep for ($j = 1; $j <= $i; $j++) so row $i prints $i values.
Printing numbers without a space makes multi-digit values run together on wider rows.
→ Append a space after each number: echo ($i + $j - 1) . " ".
Letters or empty input leave $rows uninitialized.
→ Call is_numeric(trim($line)) and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — one value, one row.
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 2 3.
Unchecked CLI input leaves $rows unset — call is_numeric(trim($line)) first.
Total prints = n(n+1)/2 — grows quadratically with row count.
Try these variations to lock in the pattern.
$i + $j with $i starting at 09 + $i + $j instead of $i + $j - 1$j = 1, $i + $j - 1 = $iis_numeric(trim($line)) until $rows >= 1$i + $j - 1. Inner loop runs $j = 1..$i — row $i prints $i numbers.echo ($i + $j - 1) . " " stays on the line; echo PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 prints a single 1.$j = 1, the value is always $i — compare with Program 34 where the formula is $i + $j.Quick Takeaway: outer loop $i = 1..$rows, inner $j = 1..$i, print $i + $j - 1, then echo PHP_EOL.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The increasing number triangle using $i + $j - 1 is a compact lesson in formula-based nested loops: compute each value with $i + $j - 1, grow the inner bound to $i, and end each row with echo PHP_EOL. Master the fixed-$rows version, then try user input and a smaller trace demo.
Practice the three examples above, then continue to Program 34 for the $i + $j variant starting from 0.
Inner bound must be $j <= $i — validate $rows when reading from the console.
for ($i = 1; $i <= $rows; $i++) in the outer loopfor ($j = 1; $j <= $i; $j++) prints $i valuesecho ($i + $j - 1) . " "is_numeric(trim($line)) before using $rowsecho PHP_EOL inside the inner loop$j <= $rows in the inner loop (prints a rectangle)$rows = 1 edge casePrint the pattern the beginner-friendly way.
$i + $j - 1
Definition$j = 1..$i
Codej=1 → i
Codeecho PHP_EOL after $j loop
ShapeO(n²) time
AnalysisEach 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.
Move on to the increasing number triangle starting from 0 ($i + $j) in the PHP number-pattern series.
12 people found this page helpful