Filled Diamond Star Pattern in PHP

What You'll Learn
This program prints a filled diamond using two halves: the upper half increases stars by 2 each row, and the lower half decreases stars by 2, keeping the pattern centered with leading spaces.
For a given row index $i, the star count is 2 * $i - 1. Total output lines are 2 * $rows - 1.
⭐ 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++) {
for ($s = 1; $s <= $rows - $i; $s++) echo " ";
for ($j = 1; $j <= 2 * $i - 1; $j++) echo "*";
echo PHP_EOL;
}
for ($i = $rows - 1; $i >= 1; $i--) {
for ($s = 1; $s <= $rows - $i; $s++) echo " ";
for ($j = 1; $j <= 2 * $i - 1; $j++) echo "*";
echo PHP_EOL;
}🧠 How It Works
Setup and upper half
$rows sets the “radius” of the diamond. for ($i = 1; $i <= $rows; $i++) prints each upper line: for ($s = 1; $s <= $rows - $i; $s++) echo " "; centers the row, then for ($j = 1; $j <= 2 * $i - 1; $j++) echo "*"; prints a solid run of odd length 1, 3, 5, …
Lower half (same row recipe)
for ($i = $rows - 1; $i >= 1; $i--) repeats the same space loop and star loop. That shrinks star counts by two per line and mirrors the top without redrawing the widest middle row.
End every line
After the loops inside each $i iteration, echo PHP_EOL; moves to the next row. Total lines: $rows + ($rows - 1) = 2 * $rows - 1. Unlike the hollow diamond (Program 9), every interior position inside the outline is *.
Cost and mobile preview
O(n²) output for n = $rows, O(1) extra space. The middle row has 2 * $rows - 1 stars; the green result strip scrolls horizontally on narrow screens.
Filled diamond
Odd-width solid rows stacked up, then down. Same centering idea as a centered triangle (Program 5), applied to both halves of the 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 = 1; $i <= $rows; $i++) {
for ($s = 1; $s <= $rows - $i; $s++) echo " ";
for ($j = 1; $j <= 2 * $i - 1; $j++) echo "*";
echo PHP_EOL;
}
for ($i = $rows - 1; $i >= 1; $i--) {
for ($s = 1; $s <= $rows - $i; $s++) echo " ";
for ($j = 1; $j <= 2 * $i - 1; $j++) echo "*";
echo PHP_EOL;
}💡 Tips for Enhancement
Try These
- Print a hollow diamond by printing stars only on the edges (Program 9)
- Use a different character (like
#) - Use
str_repeat()to build each row as a string - Validate
$rows > 0before printing - Experiment with printing spaces between stars
Avoid
- Duplicating the middle row (start the lower half from
$rows - 1) - Mixing tabs and spaces for alignment
- Forgetting newline after each row
- Using even star counts (symmetry breaks)
- Assuming user input is always valid
Key Takeaways
A filled diamond uses two halves: increasing odd stars then decreasing odd stars.
Each row prints $rows - $i spaces and 2 * $i - 1 stars.
Total output lines are 2 * $rows - 1.
Time complexity is O(n²) for n rows.
This is the filled version of the hollow diamond (Program 9).
❓ Frequently Asked Questions
$i == $rows. Starting at $rows - 1 prevents printing the middle row twice.Next: Diamond in a Frame
Continue to Program 11 to print a diamond inside a square-style frame.
A filled diamond is basically a pyramid followed by an inverted pyramid, with the middle row printed only once.
10 people found this page helpful
