An inverted right-angled triangle star pattern prints a left-aligned upside-down staircase of * characters: the first row has rows stars, and each next row has one fewer down to 1.
Remember
Rule: on outer value i, print i stars (i counts down)
*****
****
***
**
* ← 5 rows
In PHP you solve it with two nested for loops: the outer loop counts from $rows down to 1, the inner loop prints stars with echo "*", then echo PHP_EOL moves to the next line. It is the mirror of Program 1 — same inner loop, reversed outer direction.
Approach
How to Solve It
Two ways to emit the same shape — start with a countdown outer loop, then optionally use a forward formula or str_repeat.
Method
Idea
Best for
Countdown outer
$i = $rows..1, inner prints $i stars via echo "*"
Learning, interviews, clearest invert of Program 1
Forward + formula
$i = 1..$rows, print $rows - $i + 1 stars
When you prefer ascending counters
str_repeat
Build a whole row in one call while counting down
Shorter demos once loops click
Pseudocode
Pseudocode
for i from rows down to 1:
for j from 1 to i:
echo "*"
echo PHP_EOL
Cheat sheet
Goal
Pattern
Countdown rows
for ($i = $rows; $i >= 1; $i--)
Print $i stars
for ($j = 1; $j <= $i; $j++) echo "*";
End the row
echo PHP_EOL;
Forward equivalent
for ($j = 1; $j <= $rows - $i + 1; $j++)
One-line row shortcut
echo str_repeat("*", $i) . PHP_EOL; while counting down
1. One outer loop. Still walk $i from $rows down to 1.
2. Build the row.str_repeat("*", $i) creates a string of length $i filled with stars.
3. Print and advance. Concatenate PHP_EOL so the row ends correctly.
Learn the two-loop version first (Examples 1–2) so you can explain both bounds in an interview; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
Wrong direction
Upright instead of inverted
If you use $i = 1..$rows instead of $i = $rows..1, you get Program 1’s growing triangle. For the inverted shape, count down — or use $rows - $i + 1 stars with a forward loop.
PHP_EOL inside
Column of stars
If echo PHP_EOL is inside the inner loop, each star lands on its own line. Use echo "*" for stars; end the row only after the inner loop.
$j <= $rows
Rectangle, not triangle
Inner bound must be $j <= $i (or $j <= $rows - $i + 1). $j <= $rows prints a filled rectangle.
No PHP_EOL
One endless line
Omitting the row break glues every star onto a single line.
$rows = 1
Single star
Output is just * on one line — a good sanity check (same as Program 1).
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 (Example 3)
O($rows²)
O($rows) per temporary row string
Total stars printed = n + (n-1) + … + 1 = n(n+1)/2, which is still quadratic in n — identical to Program 1.
Remember
Key Takeaways
Rule: outer value $i prints exactly $i stars, with $i counting from $rows down to 1.
Same as Program 1: only the outer loop direction changes; the inner star loop stays $j = 1..$i.
Break the row: call echo PHP_EOL only after the inner loop.
Complexity:O(n²) time from the triangular star count; O(1) extra space for nested loops.
One line: for $i from $rows down to 1, print $i stars with echo, then echo PHP_EOL.
Frequently Asked Questions
The outer loop runs i from rows down to 1. For each i, the inner loop prints i stars. The first output line uses i equal to rows so it is the longest; each later line has a smaller i, so the triangle points downward.
Program 1 uses for ($i = 1; $i <= $rows; $i++) so stars grow. This program uses for ($i = $rows; $i >= 1; $i--) so stars shrink. The inner loop still runs j from 1 to i.
Yes. Use for ($i = 1; $i <= $rows; $i++) and print ($rows - $i + 1) stars in the inner loop. Both styles produce the same shape.
echo sends output without adding a line break. PHP_EOL ends the current line with a portable newline. Stars use echo; the row break uses echo PHP_EOL after the inner loop.
O(n²) for n rows. Total stars are still n(n+1)/2, same as the upright triangle.
Yes. echo str_repeat("*", $i) . PHP_EOL prints a full row in one call while i counts down.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int so bad input does not produce unexpected results.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
This inverted triangle uses the same inner loop as Program 1 — only the outer loop direction changes. Total stars stay n(n+1)/2, so complexity is still O(n²).