PHP Number Diamond Pattern

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

What Is This Pattern?

A number diamond prints ascending digits in odd-length rows that grow to a peak, then shrink back — centered with leading spaces.

Remember
Rule: top 1..levels, bottom levels-1..1; each row prints 1..(2*i-1)

    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1   ← levels = 5

In PHP use two outer loops: grow $i to $levels, then shrink. Indent with spaces, then print $k from 1 while $k < $i * 2.

How to Solve It

Build the top half, then mirror it downward — spaces first, then digits.

MethodIdeaBest for
Two-half diamondTop 1..$levels, bottom $levels-1..1Learning, interviews, exams
Levels inputSame logic with a user-chosen $levelsPractice / demos

Pseudocode

Pseudocode
for i from 1 to levels:
    print (levels - i) spaces
    print digits 1 to (2*i - 1)
    print newline
for i from levels-1 down to 1:
    print (levels - i) spaces
    print digits 1 to (2*i - 1)
    print newline

Cheat sheet

GoalPattern
Top halffor ($i = 1; $i <= $levels; $i++)
Bottom halffor ($i = $levels - 1; $i >= 1; $i--)
Top indentfor ($j = $i; $j < $levels; $j++) echo " ";
Bottom indentfor ($j = $levels; $j > $i; $j--) echo " ";
Digitsfor ($k = 1; $k < $i * 2; $k++) echo $k;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo " " / echo $kStays on the same lineEach space or digit
echo PHP_EOLEnds the current lineAfter spaces and digits on a row

Print spaces and digits without a newline, then end the row once.

Live Preview

Change the level count and the number diamond updates instantly.

Whole numbers from 1 to 5 (peak stays within single digits 1–9). Tap a chip or type a value — the preview redraws as you go.

Live result 5 levels · 9 lines
    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

Worked Walkthrough — $levels = 3

Trace spaces and digits on the way up, then back down.

Half$iSpaces / digitsPrinted row
Top12 / 11
Top21 / 123123
Top30 / 1234512345
Bottom21 / 123123
Bottom12 / 11

Total lines = 2 * $levels - 1. Digit count on row $i = 2 * $i - 1.

PHP Programs

Three complete programs: fixed 5 levels, CLI input levels, and a compact 3-level demo. Use View Output for sample results.

Example 1 — Fixed $levels = 5

Top half grows to 123456789; bottom half mirrors back to 1.

PHP
<?php
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;
}

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. Top half. $i runs 1..5; print 5 - $i spaces, then digits while $k < $i * 2.

2. Bottom half. $i runs 4..1 with the same digit rule so the shape mirrors.

3. Peak. When $i = 5, digits are 1 through 9 — the widest row.

Example 2 — User Input (levels)

Read the level count from STDIN and build both halves from that value.

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

if (!is_numeric($input) || (int) $input < 1) {
    echo "Please enter a positive whole number." . PHP_EOL;
    exit(1);
}

$levels = (int) $input;

for ($i = 1; $i <= $levels; $i++) {
    for ($j = $i; $j < $levels; $j++)
        echo " ";

    for ($k = 1; $k < $i * 2; $k++)
        echo $k;

    echo PHP_EOL;
}

for ($i = $levels - 1; $i >= 1; $i--) {
    for ($j = $levels; $j > $i; $j--)
        echo " ";

    for ($k = 1; $k < $i * 2; $k++)
        echo $k;

    echo PHP_EOL;
}

How It Works

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

2. Same diamond. Both outer loops and space bounds use $levels instead of the literal 5.

Example 3 — Compact $levels = 3

A smaller fixed demo — easier to trace by hand before scaling up.

PHP
<?php
$levels = 3;

for ($i = 1; $i <= $levels; $i++) {
    for ($j = $i; $j < $levels; $j++)
        echo " ";

    for ($k = 1; $k < $i * 2; $k++)
        echo $k;

    echo PHP_EOL;
}

for ($i = $levels - 1; $i >= 1; $i--) {
    for ($j = $levels; $j > $i; $j--)
        echo " ";

    for ($k = 1; $k < $i * 2; $k++)
        echo $k;

    echo PHP_EOL;
}

How It Works

1. Same rules. Grow, then shrink; spaces then digits on every row.

2. Quick check. Five lines total; the middle row is 12345.

Edge Cases & Pitfalls

Check these before calling the solution done.

peak twice

Bottom loop starts at $levels

The widest row prints twice. Start the bottom at $levels - 1.

no spaces

Forget the space loops

Rows left-align and the diamond shape disappears. Keep $levels - $i spaces.

$k <= $i

Print only to $i

Rows become 1, 12, 123… instead of odd lengths. Use $k < $i * 2 (or $k <= 2*$i - 1).

levels > 5

Digits past 9

echo $k prints 10, 11… and columns break. Cap at 5 for single-digit rows, or format with fixed width.

levels = 1

Single cell

Output is just 1 — bottom loop never runs.

Bad input

(int) alone is not enough

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(n²)O(1)
User input (Example 2)O(n²)O(1)

There are 2n - 1 rows and up to O(n) digits per row, so total work is quadratic in the level count.

Key Takeaways

  • Rule: grow $i to $levels, then shrink; each row prints 1..(2*$i-1).
  • Center: print $levels - $i spaces before the digits on every row.
  • echo vs PHP_EOL: spaces and digits stay on the line; echo PHP_EOL advances after each row.
  • Next step: Program 45 prints an X of stars and zeros on a grid.

One line: indent, print an odd-length ascending digit row, grow to the peak, then mirror down.

Frequently Asked Questions

A centered diamond of ascending digits: 1, 123, 12345, 1234567, 123456789, then the same rows mirrored back down to 1.
The row prints 2*$i-1 digits (odd length): 1, 3, 5, 7, 9, … using for ($k = 1; $k < $i*2; $k++).
A space loop prints leading spaces before the digits. Top half uses for ($j = $i; $j < $levels; $j++); bottom half uses for ($j = $levels; $j > $i; $j--).
The first builds the top half ($i = 1..$levels). The second mirrors back down ($i = $levels-1..1) to complete the diamond.
Program 43 is a right-aligned triangle with fixed-width columns. Program 44 is a symmetric centered diamond with odd-length digit rows.
Replace 5 with a $levels variable in both outer loops and space bounds — see Example 2.
O(n²) for n levels because each level prints O(n) digits and there are O(n) rows overall.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
Only one row prints — a single centered 1.

Did you know?

Top half runs $i = 1..$levels; bottom half mirrors $i = $levels-1..1. Each row prints 2*$i-1 digits (1 to 2*$i-1) with leading spaces to center the diamond.

Next: X Pattern with Stars and Zeros

Print * on diagonals and the center column, 0 elsewhere.

Program 45 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