Right-Angled Triangle Star Pattern in PHP

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:
*
**
***
****
*****Complete PHP Program
Fixed rows = 5 version:
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo "*";
}
echo PHP_EOL;
}🧠 How It Works
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.
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.
New line
echo PHP_EOL; ends the row (portable newline). The CLI variation can use echo str_repeat("*", $i) . PHP_EOL; instead.
Right-angled triangle
Total stars: 1+2+…+n = n(n+1)/2 — O(n²) output for n = $rows, O(1) extra memory. Long rows scroll horizontally inside the green preview on phones.
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 <= $i; $j++) {
echo "*";
}
echo PHP_EOL;
}💡 Tips for Enhancement
Try These
- Validate that
$rows > 0before 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
The outer loop controls rows, and the inner loop prints stars for the current row.
Row i prints exactly i stars, producing the right-angled triangle.
Total stars are n(n + 1)/2, so time complexity is O(n²).
This pattern is a foundation for pyramids, diamonds, hollow shapes, and mirrored patterns.
For CLI programs, fgets(STDIN) is a simple way to read input.
❓ Frequently Asked Questions
$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.str_repeat("*", $i). It keeps the same output but the code becomes shorter and often faster.$_GET or $_POST (for example, $rows = (int)($_GET["rows"] ?? 5);). This tutorial uses CLI input with STDIN because it matches typical “pattern program” exercises.Next: Inverted Right-Angled Triangle
Continue to Program 2 to print the inverted right-angled triangle pattern in PHP.
This is one of the most common pattern exercises because it teaches how the inner loop count depends on the outer loop value.
12 people found this page helpful
