Full Concentric Number Diamond in PHP

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

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:

Output
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 5
1

Complete PHP Program

Print the upper half first, then mirror it with a lower half starting from row value 2.

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

1

Set size

$k = 5; gives a diamond of width and height 2k - 1.

Setup
2

Upper half rows

for ($i = $k; $i >= 1; $i--) builds rows from outer to center.

Top half
3

Lower half rows

for ($i = 2; $i <= $k; $i++) mirrors the pattern back out from center to edges.

Bottom half
4

Cell value logic

At each position, print $j when $j > $i, else print $i to form concentric layers.

Condition
=

Full concentric diamond

The pattern has (2k-1) rows x (2k-1) columns, so complexity is O(k²).

2

Variation — User Input (CLI) Version

This version takes size input from the user (run from terminal with php):

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

1

Program 47 builds a full diamond by combining upper and lower halves.

2

Width and height are both 2k - 1 for size k.

3

The condition decides whether each cell prints edge value or layer value.

4

The same concept is useful for star and alphabet symmetry patterns.

❓ Frequently Asked Questions

The top half decreases toward center and the bottom half mirrors it outward, creating a symmetric diamond-shaped numeric gradient.
Increase $k. For example, $k = 6 prints 11 columns and 11 rows.
Yes. Remove " " from each echo, but output may be harder to read for multi-digit values.
O(n²) for size n, because total printed cells are roughly square.

Explore More PHP Number Patterns!

Practice mirrored loop logic with progressively richer number layouts.

All Number Patterns →
Did you know?

Concentric number diamonds are great for learning how to combine two mirrored phases of loops into one clean symmetric output.

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