A right-aligned right-angled triangle keeps the same star counts as Program 1, but adds leading spaces so the right edge stays flush: row i prints rows - i spaces, then i stars.
Remember
Rule: on row i, print (rows - i) spaces, then i stars
* ← 4 spaces + 1 star
**
***
****
***** ← 0 spaces + 5 stars (5 rows)
In PHP you use one outer loop for the row and two inner loops: one for spaces, one for stars, then echo PHP_EOL to move down. Every line is exactly rows characters wide. That space loop is the usual step toward centered pyramids.
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
Two inner loops
Print spaces, then stars, then PHP_EOL
Learning, interviews, exams
str_repeat
Build padding and stars as strings
Shorter demos once formulas click
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to (rows - i):
echo " "
for k from 1 to i:
echo "*"
echo PHP_EOL
2. Build the padding.str_repeat(" ", $rows - $i) creates the leading spaces (empty string when $i == $rows).
3. Build and print stars.str_repeat("*", $i) creates $i stars; concatenate PHP_EOL to end the line.
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.
No spaces
Looks like Program 1
Skipping the space loop reprints the left-aligned triangle. Print $rows - $i spaces before the stars.
Swapped bounds
Broken right edge
Spaces must be $rows - $i and stars $i. Swapping them loses the flush right edge.
$j < $rows - $i
One space short
Use $j <= $rows - $i. A strict < drops one needed space on most rows.
Tabs
Use real spaces
Print " ", not tabs — tabs break alignment across fonts and editors.
$rows = 1
Single star
0 spaces + 1 star — same as Program 1 for n = 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 space/star loops (Examples 1–2)
O($rows²)
O(1)
str_repeat (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
Rule: row $i prints $rows - $i spaces, then $i stars.
Two inner loops: padding first, then stars with echo.
Width check: every row has length $rows before echo PHP_EOL.
Complexity:O(n²) time; O(1) extra space for nested loops.
One line: for each row $i, print $rows - $i spaces, then $i stars, then echo PHP_EOL.
Frequently Asked Questions
The outer loop runs i from 1 to rows. For each row i, print (rows - i) spaces, then i stars. Row 1 has the most padding and one star; the last row has no spaces and rows stars.
Spaces and stars follow different formulas. One loop prints spaces from 1 to rows minus i; another prints stars from 1 to i. Without the space loop, output stays left-aligned like Program 1.
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.
Program 1 prints only i stars per row. Program 3 prints (rows - i) spaces first, then i stars, so the same star counts sit flush on the right.
O(n²) where n is the number of rows. Each of n rows prints about n characters (spaces plus stars).
Yes. echo str_repeat(" ", $rows - $i) . str_repeat("*", $i) . PHP_EOL builds each row without explicit inner character loops.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int so bad input does not produce unexpected results.
The right edge no longer stays flush. Keep spaces = rows - i and stars = i.
🤔
Did you know?
Right-aligned and left-aligned triangles use the same star counts per row; only leading spaces change. Each row prints exactly rows characters before the newline: (rows - i) + i = rows.