2. Trace on paper. If you start at $i = 1, the leading 0 disappears and the shape matches Program 33 less cleanly.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for max 5 or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
i = 1
Missing zero row
Starting at $i = 1 or using $i + $j - 1 drops the leading 0. Keep $i = 0..$max with $i + $j.
j <= max
Rectangle instead of triangle
Inner bound $j <= $max makes every row the same width. Use $j <= $i.
no space
Glued numbers
Without . " ", values run together (12). Always print a trailing space.
PHP_EOL inside
Broken rows
If echo PHP_EOL sits inside the inner loop, you get one number per line. Call it only after the loop.
max = 0
Single value
Output is just 0. A good sanity check for input validation.
fgets
Check 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 max = 2 (Example 3)
O(n²)
O(1)
Row $i prints i + 1 numbers. Total = 1 + 2 + … + (n+1) = (n+1)(n+2)/2 for $max = n → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Formula: each cell is $i + $j — first cell is 0 when both loops start at 0.
Zero-based width: inner loop runs $j = 0..$i so row $i has $i + 1 numbers.
Break the row: values with spaces in the inner loop; echo PHP_EOL after it finishes.
Complexity:O(n²) time from (n+1)(n+2)/2 prints; O(1) extra space.
One line: for each $i from 0 to $max, print $i + $j for $j = 0..$i with spaces, then echo PHP_EOL.
Frequently Asked Questions
An increasing triangle from 0: for max=5 you get 0 / 1 2 / 2 3 4 / 3 4 5 6 / 4 5 6 7 8 / 5 6 7 8 9 10 — each value is $i + $j with zero-based loops.
Because the loops start at $i = 0 and $j = 0, so $i + $j = 0.
$j increases from 0 to $i, so $i + $j increases by 1 each step — producing consecutive numbers.
Program 33 uses $i + $j - 1 with $i starting at 1. Program 34 uses $i + $j with $i starting at 0.
Program 34 uses the formula $i + $j per cell. Program 35 is a right-aligned continuous counter triangle.
echo ($i + $j) . " " 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 max = n because total prints are 1 + 2 + … + (n+1) = (n+1)(n+2)/2.
🤔
Did you know?
Each 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.