Hollow Diamond Star Pattern in PHP

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:
*
* *
* *
* *
* *
* *
* *
* *
*Complete PHP Program
Fixed rows = 5 version:
<?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
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;.
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.
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.
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.
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.
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;
}
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 > 0before 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
The diamond is built by printing the upper half then mirroring it as the lower half.
Each row is 2 * $rows - 1 characters wide.
Lower half starts at $rows - 1 to avoid duplicating the middle row.
Time complexity is O(n²) for n rows.
This diamond uses the same edge logic as Programs 7 and 8.
❓ Frequently Asked Questions
$rows - 1 prevents printing the middle row twice.Next: Filled Diamond Pattern
Continue to Program 10 to print a filled diamond star pattern in PHP.
The hollow diamond is a classic pattern exercise because it combines symmetry, loops, and careful boundary printing.
12 people found this page helpful
