Inverted V-Shaped Hollow 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 hollow inverted V (one star on the first row, then a widening gap between two stars). See Program 8 for the upright V (wide row first, vertex at the bottom).

Each 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;

for ($i = 1; $i <= $rows; $i++) {
    // leading spaces
    for ($s = 1; $s <= $rows - $i; $s++) {
        echo " ";
    }

    // left diagonal star
    echo "*";

    // inner gap and right diagonal star (only from row 2)
    if ($i > 1) {
        for ($g = 1; $g <= 2 * $i - 3; $g++) {
            echo " ";
        }
        echo "*";
    }

    echo PHP_EOL;
}

🧠 How It Works

1

Setup

$rows sets the height. for ($i = 1; $i <= $rows; $i++) grows the gap between the two legs from the apex to the wide base.

Setup
2

Left margin and first star

for ($s = 1; $s <= $rows - $i; $s++) { echo " "; } shifts the row right. echo "*"; draws the left leg.

Left leg
3

Gap and right star when $i > 1

if ($i > 1) { ... } runs for ($g = 1; $g <= 2 * $i - 3; $g++) { echo " "; } for the hollow interior, then echo "*"; for the right leg. Row 1 skips this block (single apex star).

Right leg
4

Finish the line

echo PHP_EOL; ends the row. Each line has width 2 * $rows - 1 characters.

Line break
=

Hollow inverted V

O(n²) output for n = $rows, O(1) extra space. Width 2n - 1 scrolls inside the green preview on phones.

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;
}

💡 Tips for Enhancement

Try These

  • Print the V-shaped hollow half by reversing the outer loop (Program 8)
  • Combine Program 7 + Program 8 to form a hollow diamond (Program 9)
  • Replace * with another character (like #)
  • Use a fixed-width font in the console for alignment
  • Validate $rows > 0 before printing

Avoid

  • Printing the second star on row 1 (it breaks the top point)
  • Mixing tabs and spaces (alignment changes)
  • Forgetting the newline after each row
  • Printing stars on every position (it becomes filled)
  • Assuming user input is always valid

Key Takeaways

1

Each row is based on a centered width of 2 * rows - 1 characters.

2

Row $i prints one star at the start, and a second star from row 2 onward.

3

The inner gap grows as 2 * $i - 3.

4

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

5

This pattern is the upper half of the hollow diamond (Program 9).

❓ Frequently Asked Questions

Only print the second star when $i > 1. This keeps the top of the inverted V as a single point.
Print this pattern for the upper half (Program 7, inverted V), then print the lower half using Program 8’s outer-loop idea starting from $rows - 1 to avoid duplicating the middle row (Program 9).
It’s O(n²) for n rows because each row prints Theta(n) characters and there are n rows.

Next: V-Shaped Hollow Pattern

Continue to Program 8 to print the upright V-shaped hollow pattern in PHP.

Program 8 →
Did you know?

If you print Program 7 and then print Program 8 starting from $rows - 1, you get the full hollow diamond (Program 9) without duplicating the middle row.

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