Centered Number Diamond in PHP

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

What You’ll Learn

How to print a centered number diamond in PHP by combining an increasing pyramid and a decreasing pyramid.

You’ll practice nested loops for leading spaces and row-wise number printing with odd-length sequences.

⭐ Pattern Output

For n = 5, the pattern looks like this:

Output
    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1
1

Complete PHP Program

First print the upper half (increasing width), then the lower half (decreasing width).

PHP
<?php
// First Part
for ($i = 1; $i <= 5; $i++) {
    for ($j = $i; $j < 5; $j++) {
        echo "  ";
    }
    for ($k = 1; $k < $i * 2; $k++) {
        echo $k;
    }
    echo PHP_EOL;
}

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

🧠 How It Works

1

Upper half loop

for ($i = 1; $i <= 5; $i++) builds rows from narrow to wide.

Top pyramid
2

Leading spaces

The space loop controls indentation so rows stay centered.

Alignment
3

Odd-width number row

for ($k = 1; $k < $i * 2; $k++) prints 2i - 1 numbers (1, 3, 5, ...).

Row content
4

Lower half loop

for ($i = 4; $i >= 1; $i--) mirrors the top, forming the diamond.

Bottom pyramid
=

Centered number diamond

Two mirrored pyramids create the full diamond. Complexity is approximately O(n²) for height n.

2

Variation — User Input (CLI) Version

This version lets users choose the height at runtime (run from terminal with php):

PHP
<?php
echo "Enter the height: ";
$n = (int) trim(fgets(STDIN));

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

for ($i = 1; $i <= $n; $i++) {
    for ($j = $i; $j < $n; $j++) {
        echo "  ";
    }
    for ($k = 1; $k < $i * 2; $k++) {
        echo $k;
    }
    echo PHP_EOL;
}

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

💡 Tips for Enhancement

Try These

  • Replace hardcoded 5 with a variable for dynamic size
  • Add spaces between numbers for wider visual separation
  • Print descending numbers for each row (e.g., 54321)
  • Swap digits with symbols to create hybrid patterns
  • Store rows in arrays before printing for reuse

Avoid

  • Changing number loop bounds without adjusting spacing
  • Forgetting to print the mirrored lower half
  • Using invalid input values without checks
  • Mixing tabs/spaces inconsistently in printed indentation

Key Takeaways

1

The pattern is built from two parts: increasing and decreasing pyramids.

2

Each row prints odd-width sequences using 2i - 1.

3

Leading spaces keep every row centered.

4

The same approach applies to star patterns and alphabet patterns.

❓ Frequently Asked Questions

The widest row appears at i = 5, and width is 2i - 1 = 9.
Increase n in the loop bounds. More rows produce a taller and wider diamond.
Yes. Replace echo $k; with echo $k . " ";, then adjust leading spaces for alignment.
O(n²) because nested loops print roughly quadratic total characters.

Explore More PHP Number Patterns!

Practice nested-loop logic by building more centered and mirrored number layouts.

All Number Patterns →
Did you know?

Diamond-like patterns are often taught to explain how row index controls both indentation and content width 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