Inverted V-Shaped Hollow Star Pattern in PHP

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:
*
* *
* *
* *
* *Complete PHP Program
Fixed rows = 5 version:
<?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
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.
Left margin and first star
for ($s = 1; $s <= $rows - $i; $s++) { echo " "; } shifts the row right. echo "*"; draws the left leg.
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).
Finish the line
echo PHP_EOL; ends the row. Each line has width 2 * $rows - 1 characters.
Hollow inverted V
O(n²) output for n = $rows, O(1) extra space. Width 2n - 1 scrolls inside the green preview on phones.
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 = 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 > 0before 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
Each row is based on a centered width of 2 * rows - 1 characters.
Row $i prints one star at the start, and a second star from row 2 onward.
The inner gap grows as 2 * $i - 3.
Time complexity is O(n²) for n rows.
This pattern is the upper half of the hollow diamond (Program 9).
❓ Frequently Asked Questions
$i > 1. This keeps the top of the inverted V as a single point.$rows - 1 to avoid duplicating the middle row (Program 9).Next: V-Shaped Hollow Pattern
Continue to Program 8 to print the upright V-shaped hollow pattern in PHP.
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.
12 people found this page helpful
