Inverted Pyramid Star Pattern in PHP

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

What You'll Learn

This program prints an inverted pyramid by decreasing the odd number of stars row by row, while increasing leading spaces to keep it centered.

Row $i prints $rows - $i spaces and 2 * $i - 1 stars (with $i decreasing from $rows to 1).

⭐ 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 = $rows; $i >= 1; $i--) {
    for ($j = 1; $j <= $rows - $i; $j++) {
        echo " ";
    }
    for ($j = 1; $j <= 2 * $i - 1; $j++) {
        echo "*";
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Setup

$rows is the height. for ($i = $rows; $i >= 1; $i--) starts with $i == $rows (widest line, 2*$rows-1 stars, no left margin) and ends at $i == 1. Inner loops match Program 5; only the outer $i loop runs backward.

Setup
2

Margin: echo " "

for ($j = 1; $j <= $rows - $i; $j++) prints $rows - $i spaces. As $i decreases each row, that count grows so the shrinking star band stays centered.

Centering
3

Stars: echo "*"

for ($j = 1; $j <= 2 * $i - 1; $j++) prints an odd run that steps down (for $rows = 5: 9, 7, 5, 3, 1). Same formula as Program 5; only $i’s sequence changed.

Width
4

New line

echo PHP_EOL; ends each row. Per-row character count is still ($rows - $i) + (2 * $i - 1) = $rows + $i - 1.

Line break
=

Inverted pyramid

Total stars still over n = $rows rows; O(n²) time, O(1) extra space. The first line is the widest—it scrolls sideways in the green preview on small 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 = $rows; $i >= 1; $i--) {
    for ($j = 1; $j <= $rows - $i; $j++) {
        echo " ";
    }
    for ($j = 1; $j <= 2 * $i - 1; $j++) {
        echo "*";
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Print the upright pyramid (Program 5) to see the mirror effect
  • Combine Program 5 + Program 6 to form a filled diamond
  • Print a hollow inverted pyramid (stars only at edges)
  • Build each row using str_repeat() for simpler code
  • Validate that $rows > 0 before printing

Avoid

  • Forgetting to increase spaces as stars decrease
  • Using even star counts (it won’t stay centered)
  • Off-by-one errors in 2 * $i - 1
  • Mixing tabs and spaces (alignment changes)
  • Assuming user input is always valid

Key Takeaways

1

Row $i prints $rows - $i spaces followed by 2 * $i - 1 stars.

2

Star counts shrink by 2 each row, keeping symmetry around the center.

3

This is the mirror of Program 5 (pyramid).

4

Time complexity is O(n²) for n rows.

5

Pyramid + inverted pyramid = filled diamond (Program 10).

❓ Frequently Asked Questions

Odd star counts keep the pyramid centered around one middle column. Each next row removes two stars, shrinking evenly on both sides.
Print Program 5 (pyramid) and then print this inverted pyramid starting from $rows - 1 to avoid repeating the middle row (Program 10).
It’s O(n²) for n rows because each row prints Theta(n) characters and there are n rows.

Next: Inverted V Hollow Pattern

Continue to Program 7 to print an inverted V-shaped hollow star pattern in PHP.

Program 7 →
Did you know?

This inverted pyramid is used as the lower half for the filled diamond (Program 10) when you print it after the upright pyramid.

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