Hollow Diamond Star Pattern in PHP

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

What You'll Learn

This pattern is Program 7 (inverted V, upper half) plus the lower half using the same outer idea as Program 8: after $i runs 1$rows, run $i from $rows - 1 down to 1 with the same hollow-gap logic.

Each printed line has width 2 * $rows - 1 (9 characters when $rows = 5).

⭐ Pattern Output

When you run the program with rows = 5:

Output
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *
1

Complete PHP Program

Fixed rows = 5 version:

PHP
<?php
$rows = 5;

// Upper half (same idea as Program 7)
for ($i = 1; $i <= $rows; $i++) {
    for ($s = 1; $s <= $rows - $i; $s++) {
        echo " ";
    }
    echo "*";
    if ($i > 1) {
        for ($g = 1; $g <= 2 * $i - 3; $g++) {
            echo " ";
        }
        echo "*";
    }
    echo PHP_EOL;
}

// Lower half (same idea as Program 8), start from rows - 1 to avoid duplicate middle row
for ($i = $rows - 1; $i >= 1; $i--) {
    for ($s = 1; $s <= $rows - $i; $s++) {
        echo " ";
    }
    echo "*";
    if ($i > 1) {
        for ($g = 1; $g <= 2 * $i - 3; $g++) {
            echo " ";
        }
        echo "*";
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Upper half (apex to widest row)

for ($i = 1; $i <= $rows; $i++) runs the same hollow row logic as Program 7: leading spaces, left *, optional gap and right *, then echo PHP_EOL;.

Upper
2

Lower half (mirror, skip duplicate middle)

for ($i = $rows - 1; $i >= 1; $i--) reuses the same inner body as the upper loop. Starting at $rows - 1 avoids printing the widest row twice.

Lower
3

Shared row body (both loops)

for ($s = 1; $s <= $rows - $i; $s++) echo " "; then echo "*";. If $i > 1, for ($g = 1; $g <= 2 * $i - 3; $g++) echo " "; and echo "*";. Every line has width 2 * $rows - 1.

Hollow
4

Line count

You print $rows + ($rows - 1) = 2 * $rows - 1 lines total. O(n²) characters for n = $rows, O(1) extra memory; wide rows scroll in the green preview on phones.

Total
=

Hollow diamond

Two back-to-back hollow V halves with identical per-row rules. Matches $rows = 5 sample output at the top of the page.

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

for ($i = $rows - 1; $i >= 1; $i--) {
    for ($s = 1; $s <= $rows - $i; $s++) echo " ";
    echo "*";
    if ($i > 1) {
        for ($g = 1; $g <= 2 * $i - 3; $g++) echo " ";
        echo "*";
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Change the character from * to # or @
  • Print a filled diamond by printing stars everywhere inside the boundary (Program 10)
  • Turn this into a box-diamond by adding a frame around the diamond (Program 11 style)
  • Use str_repeat() to build each row as a string
  • Validate $rows > 0 before printing

Avoid

  • Starting the lower half at $rows (middle row duplicates)
  • Mixing tabs and spaces (alignment breaks)
  • Forgetting newline between rows
  • Printing the second star when $i == 1
  • Assuming user input is always valid

Key Takeaways

1

The diamond is built by printing the upper half then mirroring it as the lower half.

2

Each row is 2 * $rows - 1 characters wide.

3

Lower half starts at $rows - 1 to avoid duplicating the middle row.

4

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

5

This diamond uses the same edge logic as Programs 7 and 8.

❓ Frequently Asked Questions

Because the upper half already prints the widest row. Starting from $rows - 1 prevents printing the middle row twice.
Instead of printing only the two boundary stars, print stars for all positions between them (Program 10).
It’s O(n²) because each row prints Theta(n) characters across Theta(n) rows.

Next: Filled Diamond Pattern

Continue to Program 10 to print a filled diamond star pattern in PHP.

Program 10 →
Did you know?

The hollow diamond is a classic pattern exercise because it combines symmetry, loops, and careful boundary printing.

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