Filled Diamond Star Pattern in PHP

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2n − 1 rows total

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:

Output
    *    
   ***   
  *****  
 ******* 
*********
 ******* 
  *****  
   ***   
    *    
1

Complete PHP Program

Fixed rows = 5 version:

PHP
<?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

1

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, …

Upper
2

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.

Lower
3

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 *.

Line break
4

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.

Complexity
=

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.

2

Variation — User Input (CLI) Version

Read rows from standard input (works when you run the file from terminal using php):

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 > 0 before 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

1

A filled diamond uses two halves: increasing odd stars then decreasing odd stars.

2

Each row prints $rows - $i spaces and 2 * $i - 1 stars.

3

Total output lines are 2 * $rows - 1.

4

Time complexity is O(n²) for n rows.

5

This is the filled version of the hollow diamond (Program 9).

❓ Frequently Asked Questions

Odd star counts keep the diamond centered around one middle column. Each next row changes star count by 2, expanding or shrinking evenly on both sides.
The top half already prints the widest row when $i == $rows. Starting at $rows - 1 prevents printing the middle row twice.
It’s O(n²) because there are Theta(n) lines and each line prints Theta(n) characters.

Next: Diamond in a Frame

Continue to Program 11 to print a diamond inside a square-style frame.

Program 11 →
Did you know?

A filled diamond is basically a pyramid followed by an inverted pyramid, with the middle row printed only once.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful