Concentric Descending-Ascending Number Pattern in PHP

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

What You’ll Learn

How to print a concentric descending-ascending number pattern in PHP using nested loops and if conditions.

This pattern is useful for understanding mirrored output and how row/column comparisons can shape number layouts.

⭐ 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
1

Complete PHP Program

The row value $i defines the inner zone; outer columns keep their own values.

PHP
<?php
$k = 5;
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;
}

🧠 How It Works

1

Set size value

$k = 5; controls both height and row width (2k - 1).

Setup
2

Outer loop controls rows

for ($i = $k; $i >= 1; $i--) moves from outer layer to inner layer.

Row control
3

Left and right halves

First inner loop prints from $k..1, second prints from 2..$k to mirror the row.

Symmetry
4

Conditional value selection

If $j > $i, print $j; otherwise print $i. This fills the center with lower values.

Pattern logic
=

Concentric mirrored rows

Each row has 2k-1 values, and total work is O(k²).

2

Variation — User Input (CLI) Version

This version accepts the size from user input (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;
}

💡 Tips for Enhancement

Try These

  • Allow custom separators such as commas or tabs
  • Print full square pattern (top and bottom) by mirroring rows
  • Replace numbers with letters for alphabet-style layers
  • Use ternary expressions to simplify repeated conditions
  • Store rows in an array before rendering to HTML

Avoid

  • Using invalid size values without validation
  • Changing loop bounds without keeping row symmetry
  • Printing extra spaces if exact formatting is required
  • Duplicating logic blocks without refactoring when scaling up

Key Takeaways

1

The pattern width is 2k - 1 for size k.

2

Two inner loops create left and right mirrored halves.

3

The condition picks outer values or row value to form concentric layers.

4

The same technique can be adapted to star patterns and alphabet patterns.

❓ Frequently Asked Questions

When $i = 1, all inner positions fail $j > $i, so they print 1 until the value rises again toward the right edge.
Increase $k. For example, $k = 6 prints 6 rows with width 11.
Yes. Build each row in a string/array and then trim() before printing.
O(n²) because each of n rows prints about 2n values.

Explore More PHP Number Patterns!

Build stronger loop logic by practicing mirrored and layered number outputs.

All Number Patterns →
Did you know?

Concentric number patterns are a common interview exercise to test nested-loop control and condition design in one compact problem.

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