Right-Aligned Right-Angled Triangle Star Pattern in PHP

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Spaces + stars

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:

Output
    *
   **
  ***
 ****
*****
1

Complete PHP Program

Fixed rows = 5 version:

PHP
<?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

1

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.

Setup
2

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.

Right align
3

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;.

Stars
4

New line

echo PHP_EOL; ends the row. Every row has ($rows - $i) + $i = $rows characters before the newline.

Line break
=

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.

2

Variation — User Input (CLI) Version

Read rows from standard input (works when you run the file from terminal using php):

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 > 0 after 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 - $i and $i between the two inner loops
  • Forgetting the newline after each row
  • Using $j < $rows - $i when you meant <= (off-by-one spacing)
  • Mixing tabs with spaces (alignment changes by editor)
  • Assuming input is valid without checking

Key Takeaways

1

Right alignment = print ($rows - $i) spaces, then $i stars, per row.

2

Two inner loops are used because spaces and stars have different counts each row.

3

Star counts per row match Program 1; only the leading padding changes.

4

Time complexity is O(n²) for n rows (quadratic total output).

5

This pattern is the usual stepping stone to pyramids and diamonds that mix spaces and stars.

❓ Frequently Asked Questions

For row $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.
One loop prints only one type of character. Here we need both spaces and stars with different counts per row, so we use one loop for spaces and another for stars.
The left-aligned triangle prints only stars. The right-aligned triangle prints leading spaces first, then stars, so the stars align on the right edge.
Time complexity is O(n²) for n rows because each row prints about n characters (spaces + stars) across n rows.

Next: Inverted Right-Aligned Triangle

Continue to Program 4 to print the inverted right-aligned triangle star pattern in PHP.

Program 4 →
Did you know?

Most centered patterns (pyramid, diamond) start with the same idea: print spaces first, then stars. This right-aligned triangle is a great stepping stone.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful