1. Compute both counts.$spaces = $i - 1 and $stars = $rows - $i + 1 make the invert-and-align rule obvious.
2. Build and print. Concatenate the two str_repeat results and append PHP_EOL.
3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
$j <= $i
One extra space
Space loop must be $j < $i (exactly $i - 1 spaces). $j <= $i breaks the right edge.
Program 3 formulas
Grows instead
$rows - $i spaces and 1..$i stars is Program 3. Here use $i - 1 and $rows - $i + 1.
No spaces
Left-aligned invert
Skipping the space loop gives Program 2. Right alignment needs leading spaces.
PHP_EOL inside
Column of characters
If echo PHP_EOL is inside either inner loop, each character lands on its own line. Print cells with echo; end the row only after both loops.
$rows = 1
Single star
0 spaces + 1 star — same tip case as the other triangle pages.
Bad input
Check is_numeric
Letters or empty input fail is_numeric — validate before casting to int.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O($rows²)
O(1)
str_repeat shortcut (Example 3)
O($rows²)
O($rows) per temporary row string
Each of n rows prints Θ(n) characters (spaces + stars). Star count alone is still n(n+1)/2.
Remember
Key Takeaways
Formulas:$i - 1 spaces and $rows - $i + 1 stars.
Fixed width: spaces + stars = $rows on every line.
Break the row:echo for spaces/stars; echo PHP_EOL after both loops.
Complexity:O(n²) time; O(1) extra space for nested loops.
One line: print $i - 1 spaces, then $rows - $i + 1 stars — inverted and flush right.
Frequently Asked Questions
For each row i from 1 to rows, print i minus 1 spaces, then print stars with k running from i to rows inclusive. That prints rows minus i plus 1 stars. Row 1 has no spaces and rows stars; each later row adds one space and removes one star while keeping the same right edge.
The range i through rows has length rows minus i plus 1, which matches the star count. An equivalent loop is k from 1 to rows minus i plus 1.
Program 3 uses (rows - i) spaces and stars 1 through i. Program 4 uses (i - 1) spaces and stars i through rows. Same right alignment; star counts grow in Program 3 and shrink in Program 4.
Program 2 is left-aligned with shrinking stars. Program 4 adds growing leading spaces so the same shrinking star counts stay flush on the right.
echo sends output without adding a line break. PHP_EOL ends the current line with a portable newline. Spaces and stars use echo; the row break uses echo PHP_EOL after both inner loops.
O(n²) for n rows. Each row prints on the order of n characters; there are n rows.
j from 1 to i-1 (written as j < i) prints exactly i - 1 spaces. Using j <= i would add one extra space and break the right edge.
🤔
Did you know?
This pattern merges Program 2’s shrinking star count with Program 3’s right alignment. Every row still has width rows: (i - 1) + (rows - i + 1) = rows.