PHP Diamond Diagonal Number Pattern

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

What Is This Pattern?

A diamond diagonal number pattern is Program 53’s X-shape for rows 1..$n, then the same rows again from $n − 1 down to 1 — a full diamond of diagonals.

Remember
Rule: print diagonal-mirror rows for i = 1..n
      then again for i = n-1..1

1       1
 2     2 
  3   3  
   4 4   
    5    
   4 4   
  3   3  
 2     2 
1       1     ← n = 5 (9 rows)

Unlike Program 53 (top half only), here a second outer loop mirrors the pattern vertically without duplicating the center row. Program 55 continues with a diagonal-fill number triangle.

How to Solve It

Reuse the same diagonal row logic twice: once ascending, once descending from $n − 1. End each row with echo PHP_EOL.

MethodIdeaBest for
Fixed nTwo outer loops; $i==$j / $i==$kLabs and demos
CLI inputSame logic; read $n at runtimeInteractive practice
Star diagonalsPrint * instead of the digitVisual diamond practice

Pseudocode

Pseudocode
function printRow(i, n):
    for j from 1 to n:
        if i == j: print j else print space
    for k from n-1 down to 1:
        if i == k: print k else print space
    print newline

for i from 1 to n: printRow(i, n)
for i from n-1 down to 1: printRow(i, n)

Cheat sheet

GoalPattern
Set size$n = 5;
Top halffor ($i = 1; $i <= $n; $i++)
Bottom halffor ($i = $n - 1; $i >= 1; $i--)
Main / mirrorSame as Program 53: $i==$j, $i==$k
End the rowecho PHP_EOL;
Total rows2 * $n - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $j / echo " "Stays on the same lineEach digit or space in both halves
echo PHP_EOLEnds the current lineAfter both halves of a row finish

Build each row with echo, then break once.

Live Preview

Change n and the full diamond diagonal pattern updates instantly.

Whole numbers from 3 to 7. Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · rows = 9
1       1
 2     2 
  3   3  
   4 4   
    5    
   4 4   
  3   3  
 2     2 
1       1

Worked Walkthrough

Trace $n = 3 — five rows: top 1..3, then bottom 2..1.

Half$iPrinted row
Top11 1
Top22 2
Top (center)33
Bottom22 2
Bottom11 1

The bottom loop starts at $n − 1 so the center row ($i = $n) appears only once.

PHP Programs

Three complete programs: fixed $n = 5, CLI input, and star diagonals. Use View Output for sample results.

Example 1 — Fixed $n = 5

Hard-coded size — top half 1..$n, then bottom half $n − 1..1.

PHP
<?php
$n = 5;

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

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

How It Works

1. Top half. Same as Program 53: for each $i = 1..$n, print on $i==$j and $i==$k.

2. Bottom half. Repeat the identical row logic for $i = $n − 1..1 — vertical mirror.

3. No duplicate center. Starting the second loop at $n − 1 skips reprinting the middle row.

Example 2 — User Input

Read $n from the CLI. Prefer is_numeric in real apps (shown in the tip below).

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

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

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

How It Works

1. Read n. Ask for a size, then trim(fgets(STDIN)) and cast to int.

2. Same two halves. Top 1..$n and bottom $n − 1..1 reuse the Program 53 row logic.

3. Safer input tip. Bare (int) casts turn letters into 0. Prefer:

Safer input
echo "Enter n: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
    echo "Enter a positive whole number." . PHP_EOL;
    exit(1);
}
$n = (int) $input;

Example 3 — Star Diagonals

Same diamond — print * instead of the diagonal digit in both halves.

PHP
<?php
$n = 5;

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

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

How It Works

1. Same positions. Still use $i == $j and $i == $k in both outer loops.

2. Different glyph. Print * instead of $j or $k — the diamond shape stays the same.

3. Same height. Still 2n − 1 rows total.

Edge Cases & Pitfalls

Check these before calling the solution done.

bottom from n

Duplicated center row

Starting the bottom loop at $i = $n reprints the middle row. Always start at $n − 1.

k from n

Doubled center column

Starting the mirror loop at $k = $n reprints the middle column. Keep $k = $n − 1.

missing spaces

Collapsed diamond

Skipping the space branch packs digits together and destroys both diagonals.

n = 1

Single digit

Output is just 1 — the bottom loop does not run.

n ≤ 0

Empty output

Neither outer loop runs. Guard interactive input with $n >= 1.

multi-digit

$n > 9

echo 10 prints two characters and breaks column alignment. Labs usually stick to 1–9.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input / star (Examples 1–3)O(n²)O(1)

There are 2n − 1 rows, and each row checks about 2n − 1 cells, so total work is still O(n²).

Key Takeaways

  • Rule: Program 53 rows for 1..$n, then again for $n − 1..1.
  • Skip duplicate: bottom half starts at $n − 1 so the center appears once.
  • echo vs PHP_EOL: digits and spaces stay on the line; echo PHP_EOL advances after each row.
  • Next step: Program 55 fills a triangle using diagonal jump logic.

One line: print the X for 1..$n, then mirror it for $n − 1..1.

Frequently Asked Questions

Program 53 prints only $n rows (the top half). Program 54 adds a second outer loop from $n-1 down to 1 to mirror the output vertically.
Starting at $n would duplicate the middle row (the widest point). $n-1 down to 1 mirrors without repeating the center.
For size $n, the diamond has 2*$n-1 rows: $n for the top half and $n-1 for the bottom half.
At $i=$n, both diagonals meet at the same position, so only one digit appears on the center row.
Yes. Use a variable $n — both outer loops reuse the same diagonal row logic.
Replace the printed digit with * when $i==$j or $i==$k in both outer loops — see Example 3.
echo stays on the same line for each digit or space. echo PHP_EOL ends the row after both halves of a row finish.
O(n²) for (2*$n-1) rows. Each row checks O(n) cells in two inner loops.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.

Did you know?

Program 54 prints the diagonal mirror pattern (Program 53) for rows 1..$n, then repeats the same logic for rows $n-1..1 — forming a full diamond with numbers on both diagonals.

Next: Diagonal-Fill Number Triangle

Continue with a triangle filled by diagonal jump values.

Program 55 tutorial →

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