Shape Rule
Left-shifted triangle
Row $i prints $i + 1 numbers computed as $i + $j.

The increasing number triangle using $i + $j prints 0, 1 2, 2 3 4, … — a natural follow-up after Program 33’s $i + $j - 1 triangle starting from 1. This tutorial covers the $i + $j formula, nested loops, a live preview, worked PHP examples, edge cases, and complexity.
Left-shifted triangle
Row $i prints $i + 1 numbers computed as $i + $j.
$i = 0..$max
for ($i = 0; $i <= $max; $i++) — zero-based outer loop, one growing row per iteration.
0..$i
for ($j = 0; $j <= $i; $j++) — prints $i + 1 values per row.
$i + $j
Each value is $i + $j — row $i starts at $i when $j = 0.
2–9 max
Pick a max i value and draw the zero-based increasing triangle in the browser.
Complexity
Prints per row = $i + 1 — total work scales as n².
A left-shifted increasing number triangle prints values from the formula $i + $j on each row. With $max = 5, you get 0, 1 2, 2 3 4, and so on.
In PHP you use nested loops: outer $i = 0..$max, inner $j = 0..$i, printing ($i + $j) with a trailing space.
It combines zero-based nested loops with a compact formula — a step after Program 33’s $i + $j - 1 pattern.
Formula for each value.
Zero-based grow.
When i=0, j=0 → 0.
Follow Program 33; continue to Program 35 (right-aligned counter) next.
In short: outer loop $i = 0..$max, inner $j = 0..$i, print $i + $j with a space, then echo PHP_EOL.
Given $max = 5, print a zero-based left-shifted increasing triangle: for each row $i, print $j = 0..$i values of $i + $j separated by spaces.
// $max = 5 ($i runs 0..5)
// 0
// 1 2
// 2 3 4
// 3 4 5 6
// 4 5 6 7 8
// 5 6 7 8 9 10 | Item | Type | Description |
|---|---|---|
$max | int | Maximum outer-loop value — rows run from $i = 0 to $i = $max. |
$i | int | Outer loop — current row index (starts at 0). |
$j | int | Inner loop — column index; runs 0..$i per row. |
for i from 0 to max:
for j from 0 to i:
print (i + j) + space
print newline | Approach | Idea | Best for |
|---|---|---|
| Fixed formula | 0, 1 2, … | Learning and interviews |
| User-input max | (int) trim(fgets(STDIN)); | Configurable triangle size |
| Compact trace | $max = 2 on paper first | Debugging loop bounds |
| Goal | Pattern |
|---|---|
| Outer loop | for ($i = 0; $i <= $max; $i++) |
| Inner loop | for ($j = 0; $j <= $i; $j++) |
| Print value | echo ($i + $j) . " "; |
| End the row | echo PHP_EOL; |
| User input | (int) trim(fgets(STDIN)); |
Same increasing triangle — different ways to control the max row index.
$i = 0..$maxZero-based outer loop
$i + $jStarts at 0
$j = 0..$i$i + 1 values per row
$j = 0 → $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 $i + $j - 1 — introduces zero-based loops with $i + $j.
Outer/inner bound practice with an immediate visual check.
Combine loops with ReadLine for a flexible row count.
Compare Program 33 ($i + $j - 1) and Program 35 (right-aligned counter) 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 max i value between 2 and 9 and draw the increasing triangle in the browser.
Three complete PHP programs — fixed max, user input, and a smaller trace demo. Click View Output to reveal sample console results.
Print six rows ($i = 0..5) of the increasing triangle with the $i + $j formula.
$max = 5Hard-coded maximum row index — ideal for first demos and screenshots.
<?php
for ($i = 0; $i <= 5; $i++) {
for ($j = 0; $j <= $i; $j++) {
echo ($i + $j) . " ";
}
echo PHP_EOL;
} When $i = 0, the inner loop prints 0+0 = 0. When $i = 4, it prints 4, 5, 6, 7, 8 — output 4 5 6 7 8.
Read the max row index with fgets(STDIN) instead of hard-coding 5.
Read $max with (int) trim(fgets(STDIN)) instead of hard-coding 5.
<?php
echo "Enter max i: ";
$max = (int) trim(fgets(STDIN));
if ($max < 0) return;
for ($i = 0; $i <= $max; $i++) {
for ($j = 0; $j <= $i; $j++) {
echo ($i + $j) . " ";
}
echo PHP_EOL;
} Same formula core as Example 1; only $max comes from user input instead of being hard-coded as 5. Non-numeric input leaves $max unset if you skip is_numeric() checks — always check it in safer labs.
Run with $max = 2 to trace every row on paper before scaling up.
$max = 2Same nested-loop formula with a smaller max for quick tracing.
<?php
$max = 2;
for ($i = 0; $i <= $max; $i++) {
for ($j = 0; $j <= $i; $j++) {
echo ($i + $j) . " ";
}
echo PHP_EOL;
} Only $max changes from 5 to 2 — the nested-loop formula stays identical. Trace $i = 0, 1, 2 on paper to see how each row adds one more value.
Set $max = 5; and use fgets(STDIN) when reading input. Set loop variables $i, $j.
for ($i = 0; $i <= $max; $i++) — zero-based outer loop, one growing row per iteration.
for ($j = 0; $j <= $i; $j++) — prints $i + 1 values per row.
echo ($i + $j) . " " — each value from the arithmetic formula.
echo PHP_EOL ends the row after the inner loop finishes.
Prints per row = $i + 1 — O(n²) time, O(1) extra memory.
$max = 5Trace each outer-loop value of $i, inner-loop range, values printed, and full row output.
$i | Inner range ($j) | Values (i+j) | Row output |
|---|---|---|---|
0 | 0 | 0 | 0 |
1 | 0, 1 | 1, 2 | 1 2 |
2 | 0, 1, 2 | 2, 3, 4 | 2 3 4 |
3 | 0..3 | 3, 4, 5, 6 | 3 4 5 6 |
4 | 0..4 | 4, 5, 6, 7, 8 | 4 5 6 7 8 |
5 | 0..5 | 5, 6, 7, 8, 9, 10 | 5 6 7 8 9 10 |
Prints per row = $i + 1 — total prints = ($max+1)($max+2)/2 when $i runs 0..$max.
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 <= $max and watch every row print the same width.
Foundation for formula-based triangles and left-shifted sequences starting at 1.
Example: continue to Program 35 for a right-aligned continuous counter triangle.
Practice echo vs row newline 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 $max = 5 — total is 1+2+3+4+5+6 = 21.
Pair the pattern with is_numeric(trim($line)) checks and non-negative $max validation.
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 stdio 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 $max = 2 before coding — watch how row $i starts at $i when $j = 0.
Small habits that keep number-pattern code clean.
Outer bound must be $i <= $max starting at $i = 0 — row count is $max + 1.
is_numeric(trim($line))Avoid undefined behavior when the user types letters instead of a number.
Only call echo PHP_EOL after the inner loop finishes the row.
Write the formula for each ($i, $j) pair before coding the loops.
Trace $i = 0..2 on paper before coding the full $max = 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) . " "; echo PHP_EOL only after the inner loop.
Using $i + $j - 1 or starting at $i = 1 shifts the triangle — it no longer starts at 0.
→ Keep $i + $j with $i = 0..$max and $j = 0..$i.
$j <= $max prints a rectangle — every row has the same width.
→ Keep for ($j = 0; $j <= $i; $j++) so row $i prints $i + 1 values.
Printing numbers without a space makes multi-digit values run together on wider rows.
→ Append a space after each number: echo ($i + $j) . " ".
Letters or empty input leave $max uninitialized or unchanged.
→ Call is_numeric(trim($line)) and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 0 — one value, one row.
Outer loop never runs when max < 0 — print nothing or show a message.
max < 0Treat as invalid; re-prompt instead of silent empty output.
Two rows: 0 and 1 2.
Unchecked CLI input leaves $max unset — call is_numeric(trim($line)) first.
Total prints = (max+1)(max+2)/2 — grows quadratically with max.
Try these variations to lock in the pattern.
$i + $j - 1 with $i starting at 1$j = 0, $i + $j = $iis_numeric(trim($line)) until max >= 0$i + $j. Inner loop runs $j = 0..$i — row $i prints $i + 1 numbers.echo ($i + $j) . " " stays on the line; echo PHP_EOL advances — mix them carefully.$max >= 0 for interactive programs; $max = 0 prints a single 0.$j = 0, the value is always $i — compare with Program 33 where the formula is $i + $j - 1.Quick Takeaway: outer loop $i = 0..$max, inner $j = 0..$i, print $i + $j, 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 starting from 0 is a compact lesson in zero-based nested loops: compute each value with $i + $j, grow the inner bound to $i, and end each row with echo PHP_EOL. Master the fixed-$max version, then try user input and a smaller trace demo.
Practice the three examples above, then continue to Program 35 for the right-aligned continuous counter triangle.
Outer loop must start at $i = 0 — validate $max when reading from the console.
for ($i = 0; $i <= $max; $i++) in the outer loopfor ($j = 0; $j <= $i; $j++) prints $i + 1 valuesecho ($i + $j) . " "is_numeric(trim($line)) instead of ignoring bad inputecho PHP_EOL inside the inner loop$i = 1 (skips the zero row)$j <= $max in the inner loop (prints a rectangle)$max = 0 edge casePrint the pattern the beginner-friendly way.
$i + $j
Definition$i = 0..$max
Codej=0 → i
Codeecho PHP_EOL after $j loop
ShapeO(n²) time
AnalysisEach printed value is computed as $i + $j. With $i = 0 and $j = 0 the first row prints 0; row $i = 2 prints 2, 3, 4 — a zero-based left-shifted increasing triangle.
Move on to the right-aligned continuous counter triangle in the PHP number-pattern series.
12 people found this page helpful