Full Concentric Number Diamond in PHP

What You’ll Learn
How to print a full concentric number diamond in PHP using nested loops and row/column comparisons.
You will build the top half and mirrored bottom half to create a symmetric numeric diamond.
⭐ Pattern Output
For $k = 5, the pattern looks like this:
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5Complete PHP Program
Print the upper half first, then mirror it with a lower half starting from row value 2.
<?php
$k = 5;
// First Part
for ($i = $k; $i >= 1; $i--) {
for ($j = $k; $j >= 1; $j--) {
if ($j > $i) {
echo $j . " ";
} else {
echo $i . " ";
}
}
for ($j = 2; $j <= $k; $j++) {
if ($j > $i) {
echo $j . " ";
} else {
echo $i . " ";
}
}
echo PHP_EOL;
}
// Second Part
for ($i = 2; $i <= $k; $i++) {
for ($j = $k; $j >= 1; $j--) {
if ($j > $i) {
echo $j . " ";
} else {
echo $i . " ";
}
}
for ($j = 2; $j <= $k; $j++) {
if ($j > $i) {
echo $j . " ";
} else {
echo $i . " ";
}
}
echo PHP_EOL;
}🧠 How It Works
Set size
$k = 5; gives a diamond of width and height 2k - 1.
Upper half rows
for ($i = $k; $i >= 1; $i--) builds rows from outer to center.
Lower half rows
for ($i = 2; $i <= $k; $i++) mirrors the pattern back out from center to edges.
Cell value logic
At each position, print $j when $j > $i, else print $i to form concentric layers.
Full concentric diamond
The pattern has (2k-1) rows x (2k-1) columns, so complexity is O(k²).
Variation — User Input (CLI) Version
This version takes size input from the user (run from terminal with php):
<?php
echo "Enter the size: ";
$k = (int) trim(fgets(STDIN));
if ($k < 1) {
echo "Size must be at least 1" . PHP_EOL;
exit;
}
for ($i = $k; $i >= 1; $i--) {
for ($j = $k; $j >= 1; $j--) {
echo ($j > $i ? $j : $i) . " ";
}
for ($j = 2; $j <= $k; $j++) {
echo ($j > $i ? $j : $i) . " ";
}
echo PHP_EOL;
}
for ($i = 2; $i <= $k; $i++) {
for ($j = $k; $j >= 1; $j--) {
echo ($j > $i ? $j : $i) . " ";
}
for ($j = 2; $j <= $k; $j++) {
echo ($j > $i ? $j : $i) . " ";
}
echo PHP_EOL;
}💡 Tips for Enhancement
Try These
- Convert values to strings and align columns with fixed width
- Use tabs or custom separators between numbers
- Create inverted layering by swapping condition branches
- Render pattern in HTML tables for visual styling
- Wrap repeated row logic in a reusable function
Avoid
- Forgetting the second half loop (diamond becomes incomplete)
- Using invalid input without validation
- Changing loop ranges and breaking symmetry
- Leaving trailing spaces if exact output matching is required
Key Takeaways
❓ Frequently Asked Questions
$k. For example, $k = 6 prints 11 columns and 11 rows." " from each echo, but output may be harder to read for multi-digit values.Explore More PHP Number Patterns!
Practice mirrored loop logic with progressively richer number layouts.
Concentric number diamonds are great for learning how to combine two mirrored phases of loops into one clean symmetric output.
12 people found this page helpful
