A right-angled triangle star pattern prints a left-aligned staircase of * characters: row i has exactly i stars.
Remember
Rule: on row i, print i stars
*
**
***
****
***** ← 5 rows
In PHP you solve it with two nested for loops: the outer loop picks the row, the inner loop prints stars on that row, then echo PHP_EOL; moves to the next line. Once this clicks, inverted triangles, pyramids, and hollow shapes become much easier.
Approach
How to Solve It
Two ways to emit the same shape — start with nested loops, then optionally shorten with str_repeat.
Method
Idea
Best for
Nested loops
Outer = rows, inner = stars via echo "*"
Learning, interviews, exams
str_repeat("*", $i)
Build a whole row in one call
Shorter demos once loops click
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print "*" (no newline)
print newline
2. Build the row.str_repeat("*", $i) creates a string of length $i filled with stars.
3. Print and advance. Concatenate PHP_EOL so each row ends cleanly.
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.
PHP_EOL inside
Column of stars
If echo PHP_EOL (or echo "*\n") is inside the inner loop, each star lands on its own line. Use bare echo "*" for stars; end the row only after the inner loop.
j <= rows
Rectangle, not triangle
Inner bound must be $j <= $i. $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.
rows ≤ 0
Empty output
Outer loop never runs. Validate and re-prompt for interactive programs.
Bad input
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
Nested loops (Examples 1–2)
O(rows²)
O(1)
str_repeat (Example 3)
O(rows²)
O(rows) per temporary row string
Total stars printed = 1 + 2 + … + n = n(n+1)/2, which is still quadratic in n.
Remember
Key Takeaways
Rule: row i prints exactly i stars.
Two loops: outer = rows, inner = stars with echo "*".
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 each row i, print i stars with echo, then echo PHP_EOL.
Frequently Asked Questions
The outer loop runs i from 1 to rows. For each row i, the inner loop runs j from 1 to i and prints a star with echo. Row 1 prints 1 star, row 2 prints 2 stars, and so on.
You need one loop for which row you are on and another for how many characters belong on that row. Nested for loops express that directly.
echo stays on the same line. echo PHP_EOL ends the current line. Stars use echo "*"; the row break uses echo PHP_EOL after the inner loop.
Reverse the outer loop so i runs from rows down to 1, for example for ($i = $rows; $i >= 1; $i--). The first line then has rows stars. See Program 2.
O(n²) where n is the number of rows. Total echo calls equal 1+2+…+n = n(n+1)/2.
Yes. echo str_repeat("*", $i) . PHP_EOL prints a full row in one call. Nested loops are better for learning; str_repeat is a handy shortcut later.
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?
Row i prints exactly i stars. Total stars for n rows is the triangular number n(n+1)/2 — the same count that makes this pattern O(n²).