Right-Angled Triangle Star Pattern in PHP

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
n rows total

What You'll Learn

This program prints a right-angled triangle where each next row contains one more star than the previous row.

Row i prints exactly i 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 <= $i; $j++) {
        echo "*";
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Script open and $rows

<?php starts PHP mode. $rows sets how many lines to print. for ($i = 1; $i <= $rows; $i++) walks each row index.

Setup
2

Inner loop: echo "*"

for ($j = 1; $j <= $i; $j++) runs $i times. Each echo "*"; sends another star to stdout with no line break until the row ends.

Stars
3

New line

echo PHP_EOL; ends the row (portable newline). The CLI variation can use echo str_repeat("*", $i) . PHP_EOL; instead.

Line break
=

Right-angled triangle

Total stars: 1+2+…+n = n(n+1)/2O(n²) output for n = $rows, O(1) extra memory. Long rows scroll horizontally inside the green preview on phones.

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 <= $i; $j++) {
        echo "*";
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Validate that $rows > 0 before printing
  • Print an inverted triangle by reversing the outer loop (Program 2)
  • Use str_repeat("*", $i) to build each row quickly
  • Add spaces between stars for visual variety
  • Create a hollow triangle (print only border stars)

Avoid

  • Hard-coding the row count in real apps
  • Forgetting the newline after each row
  • Assuming CLI input is always valid
  • Mixing HTML output with CLI output in the same script without intent
  • Using expensive concatenation for huge patterns (prefer building a row string)

Key Takeaways

1

The outer loop controls rows, and the inner loop prints stars for the current row.

2

Row i prints exactly i stars, producing the right-angled triangle.

3

Total stars are n(n + 1)/2, so time complexity is O(n²).

4

This pattern is a foundation for pyramids, diamonds, hollow shapes, and mirrored patterns.

5

For CLI programs, fgets(STDIN) is a simple way to read input.

❓ Frequently Asked Questions

The outer loop runs from $i = 1 to $rows. For each row $i, the inner loop runs from $j = 1 to $i, printing one star per iteration. So row 1 gets 1 star, row 2 gets 2 stars, and so on—forming the right-angled triangle.
Yes. Instead of an inner loop, you can print each row using str_repeat("*", $i). It keeps the same output but the code becomes shorter and often faster.
In a web request you can read from $_GET or $_POST (for example, $rows = (int)($_GET["rows"] ?? 5);). This tutorial uses CLI input with STDIN because it matches typical “pattern program” exercises.
The time complexity is O(n²) where n is the number of rows. The inner loop prints 1+2+3+…+n = n(n+1)/2 total stars, which simplifies to O(n²).

Next: Inverted Right-Angled Triangle

Continue to Program 2 to print the inverted right-angled triangle pattern in PHP.

Program 2 →
Did you know?

This is one of the most common pattern exercises because it teaches how the inner loop count depends on the outer loop value.

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