V-Shaped Hollow Star Pattern in PHP

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:
* *
* *
* *
* *
*Complete PHP Program
Fixed rows = 5 version:
<?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
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.
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.
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.
Finish the line
echo PHP_EOL; ends each row. Line width stays 2 * $rows - 1, same geometry as Program 7 with reversed row order.
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.
Variation — User Input (CLI) Version
Read rows from standard input (works when you run the file from terminal using 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 > 0before 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
Each row is 2 * rows - 1 characters wide.
The outer loop runs from $rows down to 1 so the diagonals move inward.
The inner gap shrinks as 2 * $i - 3.
Time complexity is O(n²) for n rows.
This is the lower-half building block for the hollow diamond (Program 9).
❓ Frequently Asked Questions
$i == 1 the inner gap becomes negative, so we print only one star. This forms the bottom vertex of the V.$rows - 1 to avoid duplicating the middle row (Program 9).Next: Hollow Diamond Pattern
Continue to Program 9 to combine the two halves into a hollow diamond.
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.
12 people found this page helpful
