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 V (wide first row, legs meet at the bottom). See Program 7 for the inverted V. It is the lower half building block for the hollow diamond (Program 9).

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 = $rows; $i >= 1; $i--) {
    // leading spaces
    for ($s = 1; $s <= $rows - $i; $s++) {
        echo " ";
    }

    // left diagonal star
    echo "*";

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

    echo PHP_EOL;
}

🧠 How It Works

1

Setup

for ($i = $rows; $i >= 1; $i--) prints the wide pair of legs first and ends at the bottom vertex. The same margin / first star / optional gap / second star structure as Program 7 applies; only $i counts down.

Setup
2

Left margin and first star

for ($s = 1; $s <= $rows - $i; $s++) { echo " "; } then echo "*";. As $i decreases, $rows - $i grows, shifting each row right.

Left leg
3

Gap and second star

if ($i > 1) prints 2 * $i - 3 spaces in a loop, then the second *. When $i == 1, only the first star and margin run—the bottom vertex.

Right leg
4

Finish the line

echo PHP_EOL; ends each row. Line width stays 2 * $rows - 1, same geometry as Program 7 with reversed row order.

Line break
=

Hollow V

Vertex at the bottom, opening upward. O(n²) output for n = $rows, O(1) extra space. Wide rows scroll horizontally in the green preview on small screens. Combine with Program 7 for a full hollow diamond.

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 ($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 inverted hollow V (Program 7) to see the mirror
  • Build a hollow diamond by printing Program 7 then printing this starting from $rows - 1 (Program 9)
  • Replace * with another symbol
  • Use fixed-width font for clean alignment
  • Validate $rows > 0 before printing

Avoid

  • Printing two stars on the last row (it breaks the bottom tip)
  • Forgetting the newline after each row
  • Mixing tabs and spaces
  • Printing stars everywhere (becomes filled)
  • Assuming user input is always valid

Key Takeaways

1

Each row is 2 * rows - 1 characters wide.

2

The outer loop runs from $rows down to 1 so the diagonals move inward.

3

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

4

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

5

This is the lower-half building block for the hollow diamond (Program 9).

❓ Frequently Asked Questions

Because when $i == 1 the inner gap becomes negative, so we print only one star. This forms the bottom vertex of the V.
Print Program 7 (upper half), then print this outer loop starting from $rows - 1 to avoid duplicating the middle row (Program 9).
It’s O(n²) because each row prints Theta(n) characters across n rows.

Next: Hollow Diamond Pattern

Continue to Program 9 to combine the two halves into a hollow diamond.

Program 9 →
Did you know?

If you start this descending row loop from $rows - 1, you get the lower half of the hollow diamond without printing the middle row twice.

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