Right-Aligned Right-Angled Triangle Star Pattern in PHP

What You'll Learn
How to print a right-aligned right-angled triangle in PHP: you print ($rows - $i) spaces first and then print $i stars, so the stars line up on the right edge.
With $rows = 5, the first row has four spaces and one star; the last row has no spaces and five stars.
⭐ Pattern Output
When you run the program with rows = 5:
*
**
***
****
*****Complete PHP Program
Fixed rows = 5 version:
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $rows - $i; $j++) {
echo " ";
}
for ($k = 1; $k <= $i; $k++) {
echo "*";
}
echo PHP_EOL;
}🧠 How It Works
Setup
$rows is the height. $i indexes the row; $j runs the space loop; $k runs the star loop. Nested for loops follow the opening <?php tag.
Padding: echo " "
for ($j = 1; $j <= $rows - $i; $j++) { echo " "; } prints $rows - $i spaces so the star block shifts right while the right edge stays straight.
Stars: echo "*"
for ($k = 1; $k <= $i; $k++) { echo "*"; } prints $i asterisks after the padding—same star count per row as Program 1, different offset. You can also echo str_repeat(" ", $rows - $i) . str_repeat("*", $i) . PHP_EOL;.
New line
echo PHP_EOL; ends the row. Every row has ($rows - $i) + $i = $rows characters before the newline.
Right-aligned triangle
Star total n(n+1)/2; O(n²) output for n = $rows, O(1) extra space. Full-width lines scroll horizontally in the green preview on narrow viewports.
Variation — User Input (CLI) Version
Read rows from standard input (works when you run the file from terminal using php):
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $rows - $i; $j++) {
echo " ";
}
for ($k = 1; $k <= $i; $k++) {
echo "*";
}
echo PHP_EOL;
}💡 Tips for Enhancement
Try These
- Validate
$rows > 0after reading input - Remove the space loop to get the left-aligned triangle (Program 1)
- Try the inverted right-aligned version (Program 4) by reversing the outer loop
- Print digits instead of
*for a right-aligned number triangle - Try a hollow right-aligned triangle (border only)
Avoid
- Swapping
$rows - $iand$ibetween the two inner loops - Forgetting the newline after each row
- Using
$j < $rows - $iwhen you meant<=(off-by-one spacing) - Mixing tabs with spaces (alignment changes by editor)
- Assuming input is valid without checking
Key Takeaways
Right alignment = print ($rows - $i) spaces, then $i stars, per row.
Two inner loops are used because spaces and stars have different counts each row.
Star counts per row match Program 1; only the leading padding changes.
Time complexity is O(n²) for n rows (quadratic total output).
This pattern is the usual stepping stone to pyramids and diamonds that mix spaces and stars.
❓ Frequently Asked Questions
$i, print ($rows - $i) spaces, then $i stars. The spaces push the stars to the right so the hypotenuse lines up on the right side.Next: Inverted Right-Aligned Triangle
Continue to Program 4 to print the inverted right-aligned triangle star pattern in PHP.
Most centered patterns (pyramid, diamond) start with the same idea: print spaces first, then stars. This right-aligned triangle is a great stepping stone.
12 people found this page helpful
