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 a centered pyramid by printing spaces first and then an odd number of stars.

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

🧠 How It Works

1

Setup

$rows is the pyramid height. for ($i = 1; $i <= $rows; $i++) walks from one star on row 1 to a bottom row of 2 * $rows - 1 stars. Two inner loops both use $j—first spaces, then stars.

Setup
2

Margin: echo " "

for ($j = 1; $j <= $rows - $i; $j++) prints $rows - $i spaces so the odd-width star block sits centered.

Centering
3

Stars: echo "*"

for ($j = 1; $j <= 2 * $i - 1; $j++) prints 1, 3, 5, …, 2*$rows-1 asterisks. Or echo str_repeat(" ", $rows - $i) . str_repeat("*", 2 * $i - 1) . PHP_EOL;.

Width
4

New line

echo PHP_EOL; ends the row. Row $i prints ($rows - $i) + (2 * $i - 1) = $rows + $i - 1 characters before the newline.

Line break
=

Symmetric pyramid

Total stars (sum of odd lengths 1 through 2n-1); O(n²) output for n = $rows, O(1) extra space. The bottom row scrolls horizontally in the green preview on narrow screens. Same core loops as the top half of Program 10.

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

💡 Tips for Enhancement

Try These

  • Print the inverted pyramid by reversing the outer loop and adjusting star counts (Program 6)
  • Combine pyramid + inverted pyramid to form a filled diamond
  • Build each row using str_repeat() for simpler code
  • Print a hollow pyramid (stars only at the edges)
  • Validate $rows > 0 before printing

Avoid

  • Using even star counts (the pyramid won’t stay centered)
  • Forgetting spaces before stars (pattern shifts left)
  • Off-by-one mistakes in 2 * $i - 1
  • Mixing tabs and spaces (alignment changes by font)
  • Assuming user input is always valid

Key Takeaways

1

Centering uses $rows - $i spaces before the stars.

2

Row $i prints 2 * $i - 1 stars (odd counts keep symmetry).

3

Each next row adds 2 stars and removes 1 leading space.

4

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

5

This is the building block for diamonds (pyramid + inverted pyramid).

❓ Frequently Asked Questions

Odd star counts (\(1,3,5,\dots\)) keep the pyramid perfectly centered around a single middle column. Each new row adds two stars, expanding equally on both sides.
Print stars only at the left and right edges of each row, and print a full row of stars at the base. Everything else becomes spaces.
It’s O(n²) for n rows because each row prints Theta(n) characters and there are n rows.

Next: Inverted Pyramid Pattern

Continue to Program 6 to print the inverted pyramid star pattern in PHP.

Program 6 →
Did you know?

If you print this pyramid and then print Program 6, you get a filled diamond (Program 10).

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