Mirrored Diagonal Number Pattern in PHP

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

What You’ll Learn

How to print a mirrored diagonal number pattern in PHP using nested loops and equality checks.

Each row prints the row number on left and right diagonals, with spaces in between.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
1       1
  2     2
    3   3
      4 4
        5
1

Complete PHP Program

First loop prints the left diagonal zone, second loop prints the mirrored right zone.

PHP
<?php
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;
}

🧠 How It Works

1

Outer loop controls rows

for ($i = 1; $i <= 5; $i++) determines which row value to place.

Row control
2

Left-side scan

Loop $j = 1..5; print number only when $i == $j, else spaces.

Left diagonal
3

Right-side mirror

Loop $k = 4..1; print when $i == $k, else spaces for mirrored diagonal.

Right diagonal
4

Line break

echo PHP_EOL; moves to the next row.

Formatting
=

Mirrored diagonals

The pattern checks two diagonal positions per row, 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;
}

💡 Tips for Enhancement

Try These

  • Use stars or letters instead of numbers on diagonals
  • Increase spacing width for better alignment with larger values
  • Print full X-shape by extending rows beyond midpoint
  • Colorize diagonal values in HTML output mode
  • Convert pattern lines into array for testing/assertions

Avoid

  • Using non-breaking HTML spaces in CLI-focused code
  • Forgetting to adjust right loop bounds when size changes
  • Printing both diagonals twice at center intersection
  • Skipping input validation for row count

Key Takeaways

1

Row index equality checks place numbers on diagonals.

2

Two inner loops create left and mirrored right halves.

3

Center row merges into a single printed value.

4

Same approach adapts to symbol-based and X-shaped patterns.

❓ Frequently Asked Questions

At the last row in this layout, both diagonal conditions meet toward the same position, so one visible value remains.
Set $n = 8 (or input 8 in the CLI version) and loops adapt automatically.
Yes. Replace " " with ". " to visualize empty cells.
O(n²) due to nested loops over row and column positions.

Explore More PHP Number Patterns!

Practice diagonal logic with more creative number-layout challenges.

All Number Patterns →
Did you know?

Diagonal patterns are a classic exercise for visualizing matrix coordinates and understanding row/column index relationships.

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