PHP Concentric Number Diamond Pattern

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A concentric number diamond is Program 46’s square mirrored top-to-bottom: layers from $k down to 1, then back out to $k.

Remember
Rule: top i=k..1, bottom i=2..k; cell = (j > i ? j : i)

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   ← k = 5

Same row logic as Program 46 (left $k..1, right 2..$k). Add a second outer loop $i = 2..$k to complete the diamond.

How to Solve It

Reuse the square’s row builder twice — once going in, once going out.

MethodIdeaBest for
Two-half diamondTop $k..1, bottom 2..$k, same cell ruleLearning, interviews, exams
Input $kSame logic with a user-chosen peakPractice / demos

Pseudocode

Pseudocode
for i from k down to 1:
    print left half j = k..1 and right half j = 2..k
    (value = j if j > i else i)
for i from 2 to k:
    print the same row rule again
    print newline after each row

Cheat sheet

GoalPattern
Top halffor ($i = $k; $i >= 1; $i--)
Bottom halffor ($i = 2; $i <= $k; $i++)
Left / right$j = $k..1 / $j = 2..$k
Cell valueecho ($j > $i ? $j : $i) . " ";
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo ($j > $i ? $j : $i) . " "Stays on the same lineEach number plus a trailing space
echo PHP_EOLEnds the current lineAfter both half-loops finish a row

Print numbers without a newline, then end the row once.

Live Preview

Change $k and the concentric diamond updates instantly.

Whole numbers from 1 to 7. Size = (2k - 1) × (2k - 1). Tap a chip or type a value — the preview redraws as you go.

Live result k = 5 · 9 × 9
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 

Worked Walkthrough — $k = 3

Top half walks in; bottom half walks out (skip repeating the center).

Half$iPrinted row
Top33 3 3 3 3
Top23 2 2 2 3
Top13 2 1 2 3
Bottom23 2 2 2 3
Bottom33 3 3 3 3

Total lines = 2*$k - 1. Cells for $k = 5 = 9 × 9 = 81.

PHP Programs

Three complete programs: fixed $k = 5, user-input $k, and a compact $k = 3 demo. Use View Output for sample results.

Example 1 — Fixed $k = 5

Top half then bottom half — same row builder twice.

PHP
<?php
$k = 5;

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;
}

How It Works

1. Top half. $i runs from 5 down to 1 — same concentric rows as Program 46.

2. Bottom half. $i runs from 2 up to 5 — mirrors outward without repeating the center.

3. Cell rule. Every print uses $j > $i ? $j : $i on left $k..1 and right 2..$k.

Example 2 — User Input ($k)

Read $k; both halves and width scale automatically.

PHP
<?php
echo "Enter k: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
    echo "Please enter a positive whole number." . PHP_EOL;
    exit(1);
}

$k = (int) $input;

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;
}

How It Works

1. Validate k. Check is_numeric; require $k >= 1.

2. Same diamond. Size becomes (2*$k - 1) × (2*$k - 1).

Example 3 — Compact $k = 3

A smaller fixed demo — five lines, easy to check by hand.

PHP
<?php
$k = 3;

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;
}

How It Works

1. Same rules. Grow in, then out; cell value is always $j > $i ? $j : $i.

2. Quick check. Center row is 3 2 1 2 3; first and last rows are all 3s.

Edge Cases & Pitfalls

Check these before calling the solution done.

i = 1 twice

Bottom loop starts at $i = 1

The center row prints twice. Start the bottom half at $i = 2.

no bottom

Forget the second outer loop

You get Program 46’s square only. Add for ($i = 2; $i <= $k; $i++).

j = 1

Right half starts at $j = 1

The center digit doubles on every row. Keep $j = 2..$k.

k = 1

Single cell

Output is just 1 — the bottom loop never runs.

PHP_EOL

echo PHP_EOL inside a half-loop

That breaks the row. Call echo PHP_EOL only after both inner loops.

Bad input

Casting without checks

Prefer is_numeric after trim(fgets(STDIN)) so non-numeric input does not silently become 0.

Time and Space Complexity

ProgramTimeExtra space
Fixed / compact (Examples 1, 3)O($k²)O(1)
User input (Example 2)O($k²)O(1)

The grid is (2*$k - 1) × (2*$k - 1), so total prints are about 4*$k² — quadratic in $k.

Key Takeaways

  • Rule: top $k..1, bottom 2..$k; cell = $j > $i ? $j : $i.
  • Shape: Program 46’s square plus a mirror — size (2*$k - 1)².
  • echo vs PHP_EOL: numbers stay on the line; echo PHP_EOL advances after both halves.
  • Next step: Program 48 prints the powers-of-11 sequence (1, 11, 121, …).

One line: print concentric rows inward to 1, then mirror them outward to finish the diamond.

Frequently Asked Questions

A full concentric number diamond: outer layer k, values decrease to 1 at the center, then increase back to k — a (2k-1)×(2k-1) grid.
The first prints the top half ($i = $k down to 1). The second prints the bottom half ($i = 2 up to $k) to mirror the shape.
Program 46 prints only the top half (k rows). Program 47 adds the bottom half loop to form a complete diamond.
When column $j is still outside the current row layer $i, print $j. Otherwise print $i (the current layer value).
Each row has 2*$k - 1 numbers. With $k = 5, width and height are both 9.
Change $k or read it from input — both outer loops and row width adjust automatically — see Example 2.
O(k²) because the grid has about (2k-1)² cells and each is printed once.
The center prints 1 — the deepest layer of the concentric pattern.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.

Did you know?

Top half: for ($i = $k; $i >= 1; $i--). Bottom half: for ($i = 2; $i <= $k; $i++). Each cell: $j > $i ? $j : $i. Grid size = 2k - 1 rows and columns.

Next: Powers of 11 Pattern

Print 1, 11, 121, 1331, 14641 with a running multiply-by-11 state.

Program 48 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

6 people found this page helpful