Diamond Mirrored Diagonal Pattern in PHP

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:
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1Complete PHP Program
First print the top mirrored diagonal, then print the bottom mirrored diagonal by reversing row flow.
<?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
Top half rows
for ($i = 1; $i <= 5; $i++) prints the upper mirrored diagonal shape.
Left and right checks
Two inner loops print value on diagonals and spaces elsewhere to maintain shape.
Bottom half rows
for ($i = 4; $i >= 1; $i--) mirrors the upper half downward without repeating center row.
Row separation
echo PHP_EOL; after each row keeps the diamond aligned line-by-line.
Full mirrored diamond
Upper + lower halves create a symmetric diamond with complexity O(n²).
Variation — User Input (CLI) Version
This version accepts row size from user input (run from terminal with 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
Program 54 combines top and bottom halves into one diamond.
Diagonal equality checks place numbers precisely.
Center row should print once for perfect symmetry.
The same approach supports many mirrored pattern designs.
❓ Frequently Asked Questions
$n in the user-input version. Width and height grow automatically.Explore More PHP Number Patterns!
Keep practicing mirrored and diagonal logic with richer pattern exercises.
Diamond-style patterns are a common interview-style exercise for testing nested-loop control and symmetry reasoning at the same time.
12 people found this page helpful
