0
0
PhpProgramBeginner · 2 min read

PHP Program to Print Pyramid Pattern

Use nested for loops in PHP to print spaces and stars for each row, like for ($i = 1; $i <= $rows; $i++) { for ($j = $rows; $j > $i; $j--) echo ' '; for ($k = 1; $k <= 2 * $i - 1; $k++) echo '*'; echo "\n"; } to print a pyramid pattern.
📋

Examples

Input3
Output * *** *****
Input5
Output * *** ***** ******* *********
Input1
Output*
🧠

How to Think About It

To print a pyramid pattern, think of each row having spaces first, then stars. The number of spaces decreases each row, and the number of stars increases by two. Use one loop for rows, one loop for spaces, and one loop for stars.
📐

Algorithm

1
Get the number of rows as input.
2
For each row from 1 to number of rows:
3
Print spaces equal to (total rows - current row).
4
Print stars equal to (2 * current row - 1).
5
Move to the next line after printing stars.
💻

Code

php
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
    for ($j = $rows; $j > $i; $j--) {
        echo ' ';
    }
    for ($k = 1; $k <= 2 * $i - 1; $k++) {
        echo '*';
    }
    echo "\n";
}
?>
Output
* *** ***** ******* *********
🔍

Dry Run

Let's trace the pyramid pattern for 3 rows through the code.

1

Row 1

Spaces: 2 (because 3 - 1), Stars: 1 (2*1 - 1), Output: ' *'

2

Row 2

Spaces: 1 (3 - 2), Stars: 3 (2*2 - 1), Output: ' ***'

3

Row 3

Spaces: 0 (3 - 3), Stars: 5 (2*3 - 1), Output: '*****'

RowSpacesStarsOutput
121 *
213 ***
305*****
💡

Why This Works

Step 1: Print spaces first

We print spaces first to push stars to the right, creating the pyramid shape. The spaces decrease each row using $rows - $i.

Step 2: Print stars next

Stars increase by two each row using 2 * $i - 1, making the pyramid wider as we go down.

Step 3: Move to next line

After printing spaces and stars for a row, we print a newline \n to start the next row below.

🔄

Alternative Approaches

Using while loops
php
<?php
$rows = 5;
$i = 1;
while ($i <= $rows) {
    $j = $rows;
    while ($j > $i) {
        echo ' ';
        $j--;
    }
    $k = 1;
    while ($k <= 2 * $i - 1) {
        echo '*';
        $k++;
    }
    echo "\n";
    $i++;
}
?>
Uses while loops instead of for loops; slightly longer but same logic.
Using str_repeat function
php
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
    echo str_repeat(' ', $rows - $i);
    echo str_repeat('*', 2 * $i - 1);
    echo "\n";
}
?>
Simplifies printing spaces and stars using built-in <code>str_repeat</code> function.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops: one for rows and two inner loops for spaces and stars, each up to n, resulting in O(n^2) time.

Space Complexity

The program uses constant extra space for counters and prints directly, so space complexity is O(1).

Which Approach is Fastest?

Using str_repeat is more concise and may be slightly faster due to internal optimizations, but all approaches have similar O(n^2) time.

ApproachTimeSpaceBest For
For loopsO(n^2)O(1)Clear control and beginner-friendly
While loopsO(n^2)O(1)Alternative loop style
str_repeat functionO(n^2)O(1)Concise and readable code
💡
Use nested loops carefully: outer for rows, inner for spaces and stars.
⚠️
Beginners often forget to decrease spaces or increase stars correctly, breaking the pyramid shape.