Diamond Mirrored Diagonal Pattern in PHP

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops + Condition

What You’ll Learn

How to print a diamond mirrored diagonal number pattern in PHP by combining an upper and lower diagonal half.

This helps practice symmetry and reuse of the same row-building logic in opposite directions.

⭐ Pattern Output

For 5 rows, the full diamond looks like this:

Output
1       1
  2     2
    3   3
      4 4
        5
      4 4
    3   3
  2     2
1       1
1

Complete PHP Program

First print the top mirrored diagonal, then print the bottom mirrored diagonal by reversing row flow.

PHP
<?php
// First Part
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= 5; $j++) {
        if ($i == $j) {
            echo $j;
        } else {
            echo "  ";
        }
    }
    for ($k = 4; $k >= 1; $k--) {
        if ($i == $k) {
            echo $k;
        } else {
            echo "  ";
        }
    }
    echo PHP_EOL;
}

// Second Part
for ($i = 4; $i >= 1; $i--) {
    for ($j = 1; $j <= 5; $j++) {
        if ($i == $j) {
            echo $j;
        } else {
            echo "  ";
        }
    }
    for ($k = 4; $k >= 1; $k--) {
        if ($i == $k) {
            echo $k;
        } else {
            echo "  ";
        }
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Top half rows

for ($i = 1; $i <= 5; $i++) prints the upper mirrored diagonal shape.

Upper half
2

Left and right checks

Two inner loops print value on diagonals and spaces elsewhere to maintain shape.

Diagonal logic
3

Bottom half rows

for ($i = 4; $i >= 1; $i--) mirrors the upper half downward without repeating center row.

Lower half
4

Row separation

echo PHP_EOL; after each row keeps the diamond aligned line-by-line.

Formatting
=

Full mirrored diamond

Upper + lower halves create a symmetric diamond with complexity O(n²).

2

Variation — User Input (CLI) Version

This version accepts row size from user input (run from terminal with php):

PHP
<?php
echo "Enter number of rows: ";
$n = (int) trim(fgets(STDIN));

if ($n < 1) {
    echo "Rows must be at least 1" . PHP_EOL;
    exit;
}

for ($i = 1; $i <= $n; $i++) {
    for ($j = 1; $j <= $n; $j++) {
        echo ($i == $j) ? $j : "  ";
    }
    for ($k = $n - 1; $k >= 1; $k--) {
        echo ($i == $k) ? $k : "  ";
    }
    echo PHP_EOL;
}

for ($i = $n - 1; $i >= 1; $i--) {
    for ($j = 1; $j <= $n; $j++) {
        echo ($i == $j) ? $j : "  ";
    }
    for ($k = $n - 1; $k >= 1; $k--) {
        echo ($i == $k) ? $k : "  ";
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Replace numbers with stars/alphabet for visual variants
  • Adjust space width for better multi-digit alignment
  • Print only outline or fill interior with symbols
  • Convert output to HTML preformatted block for display
  • Use helper function to avoid repeating row logic

Avoid

  • Repeating center row in both halves unintentionally
  • Using HTML-only spacing entities in CLI examples
  • Forgetting to keep left and right loops symmetric
  • Skipping input validation for dynamic size

Key Takeaways

1

Program 54 combines top and bottom halves into one diamond.

2

Diagonal equality checks place numbers precisely.

3

Center row should print once for perfect symmetry.

4

The same approach supports many mirrored pattern designs.

❓ Frequently Asked Questions

The first half expands diagonally toward center, and the second half contracts in reverse, creating a diamond outline.
Increase $n in the user-input version. Width and height grow automatically.
Yes. Remove either left or right diagonal printing logic to retain one side.
O(n²) because each row performs O(n) position checks over roughly 2n rows.

Explore More PHP Number Patterns!

Keep practicing mirrored and diagonal logic with richer pattern exercises.

All Number Patterns →
Did you know?

Diamond-style patterns are a common interview-style exercise for testing nested-loop control and symmetry reasoning at the same time.

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.

12 people found this page helpful