Hollow Diamond Inside Square Star Pattern in PHP

What You'll Learn
You print a frame with a solid row of * on the first line and again on the last line, and mirror the rows in between so the middle row is narrowest (only the left and right border stars with a wide hollow gap).
The width is 2 * rows; the number of lines is 2 * rows - 1. This layout differs from the standalone hollow diamond outline (Program 9).
⭐ Pattern Output
When you run the program with rows = 5:
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********Complete PHP Program
Fixed rows = 5 version:
<?php
$rows = 5;
$height = 2 * $rows - 1;
$width = 2 * $rows;
for ($line = 1; $line <= $height; $line++) {
if ($line == 1 || $line == $height) {
for ($j = 1; $j <= $width; $j++) echo "*";
} else {
$i = ($line <= $rows) ? $line : (2 * $rows - $line);
$left = $rows - $i + 1;
$gap = 2 * ($i - 1);
for ($j = 1; $j <= $left; $j++) echo "*";
for ($j = 1; $j <= $gap; $j++) echo " ";
for ($j = 1; $j <= $left; $j++) echo "*";
}
echo PHP_EOL;
}🧠 How It Works
Grid size
$height = 2 * $rows - 1; is the number of lines. $width = 2 * $rows; is the number of characters per full bar row. The shape is wider than the standalone hollow diamond (Program 9) because the frame uses an even width.
Top and bottom frame rows
for ($line = 1; $line <= $height; $line++) visits every line. When $line == 1 or $line == $height, for ($j = 1; $j <= $width; $j++) echo "*"; prints a solid horizontal edge.
Middle rows: mirror index, left stars, gap, right stars
Otherwise compute $i = ($line <= $rows) ? $line : (2 * $rows - $line); so rows equidistant from the top and bottom share the same pattern. Then $left = $rows - $i + 1; and $gap = 2 * ($i - 1);. Print $left stars, $gap spaces, then $left stars again—the hollow diamond cavity between the two vertical star runs.
Finish the line
Every branch ends with echo PHP_EOL;. O(n²) output for n = $rows, O(1) extra space. Lines of length 2 * $rows scroll horizontally in the green preview on phones.
Diamond in a frame
Solid top and bottom bars cap a hollow diamond carved from paired star columns. Matches $rows = 5 sample output (first lines shown).
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));
$height = 2 * $rows - 1;
$width = 2 * $rows;
for ($line = 1; $line <= $height; $line++) {
if ($line == 1 || $line == $height) {
for ($j = 1; $j <= $width; $j++) echo "*";
} else {
$i = ($line <= $rows) ? $line : (2 * $rows - $line);
$left = $rows - $i + 1;
$gap = 2 * ($i - 1);
for ($j = 1; $j <= $left; $j++) echo "*";
for ($j = 1; $j <= $gap; $j++) echo " ";
for ($j = 1; $j <= $left; $j++) echo "*";
}
echo PHP_EOL;
}💡 Tips for Enhancement
Try These
- Validate
$rows > 0before printing - Print only the hollow diamond outline (Program 9) for comparison
- Change the frame character (e.g., use
#) - Use
str_repeat()to build each line as a string - Try making the frame hollow (border only) instead of solid top/bottom bars
Avoid
- Using
2 * $rows - 1as width (this pattern uses2 * $rows) - Forgetting to mirror
$ifor the bottom half - Mixing tabs and spaces (alignment breaks)
- Forgetting newline after each line
- Assuming user input is always valid
Key Takeaways
Width is 2 * $rows and height is 2 * $rows - 1.
Top and bottom lines are fully filled with stars (frame).
Middle lines are symmetric using mirrored $i values.
Time complexity is O(n²) for n rows.
This is a different layout than the standalone hollow diamond (Program 9).
❓ Frequently Asked Questions
2 * $rows and height 2 * $rows - 1 so the top and bottom can be solid horizontal bars while the inner rows close on the left and right sides.2 * $rows - 1. Program 11 adds a frame-like look using width 2 * $rows and solid top/bottom rows.Explore: All Star Patterns
Browse the full PHP star pattern series.
This pattern is a popular variation because it combines a frame-like look with a symmetric hollow diamond.
9 people found this page helpful
