PHP Palindromic Number Pyramid Pattern

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

What Is This Pattern?

A palindromic number pyramid centers each row with leading spaces, then prints 1..$i followed by $i − 1..1 — every row reads the same forwards and backwards.

Remember
Rule: for i = 1..rows
        print leading "  " (rows-i+1 times)
        print 1..i
        print i-1..1  (via --n)

          1 
        1 2 1 
      1 2 3 2 1 
    1 2 3 4 3 2 1 
  1 2 3 4 5 4 3 2 1     ← rows = 5

Unlike Program 55 (left-aligned diagonal fill), here three parts per row — spaces, climb, descend — build a centered palindrome. Program 57 continues with a hollow number pyramid.

How to Solve It

Pad with spaces, print ascending 1..$i, then descend with --$n for $i − 1 steps. End each row with echo PHP_EOL.

MethodIdeaBest for
Fixed rowsHard-code size; three loops per rowLabs and demos
CLI inputSame logic; read $rows at runtimeInteractive practice
Compact rowsimplode without trailing spaceCleaner console output

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from rows down to i:
        print "  "
    for k from 1 to i:
        print k and a space
    n = i
    for m from 1 to i-1:
        n = n - 1
        print n and a space
    print newline

Cheat sheet

GoalPattern
Set size$rows = 5;
Leading spacesfor ($j = $rows; $j >= $i; $j--) echo " ";
Climbfor ($k = 1; $k <= $i; $k++) echo $k . " ";
Descend$n = $i; for ($m = 1; $m < $i; $m++) echo --$n . " ";
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo " " / echo $k . " "Stays on the same lineSpaces and each number in the row
echo PHP_EOLEnds the current lineAfter all three parts finish

Build the full centered row with echo, then break once.

Live Preview

Change the row count and the palindromic pyramid updates instantly.

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

Live result rows = 5 · peak = 5
          1 
        1 2 1 
      1 2 3 2 1 
    1 2 3 4 3 2 1 
  1 2 3 4 5 4 3 2 1 

Worked Walkthrough

Trace $rows = 5, row $i = 3 — the three parts that build 1 2 3 2 1.

PartLoopResult
Spaces$j = 5..3 → three " "
Climb$k = 1..31 2 3
Descend$n = 3; --$n twice2 1

Combined: 1 2 3 2 1. The peak 3 appears once — descent starts at $i − 1.

PHP Programs

Three complete programs: fixed $rows = 5, CLI input, and compact implode rows. Use View Output for sample results.

Example 1 — Fixed $rows = 5

Hard-coded size — leading spaces, climb 1..$i, descend with --$n.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = $rows; $j >= $i; $j--) {
        echo "  ";
    }
    for ($k = 1; $k <= $i; $k++) {
        echo $k . " ";
    }

    $n = $i;
    for ($m = 1; $m < $i; $m++) {
        echo --$n . " ";
    }

    echo PHP_EOL;
}
?>

How It Works

1. Center. Print " " from $rows down to $i so each row sits under the previous one.

2. Climb. Print 1..$i with a space after each number.

3. Descend. Set $n = $i, then --$n for $i − 1 steps so the peak is not duplicated.

Example 2 — User Input

Read $rows from the CLI. Prefer is_numeric (shown below).

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

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

How It Works

1. Validate input. is_numeric rejects letters; require a positive whole number.

2. Same three parts. Spaces, climb, and descend match Example 1 — only the source of $rows changes.

3. Scale padding. The space loop uses $rows, so centering stays correct for any height.

Example 3 — Compact Rows

Same logic; use str_repeat and implode for clean rows without a trailing space.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    $spaces = str_repeat("  ", $rows - $i + 1);
    $parts = [];

    for ($k = 1; $k <= $i; $k++) {
        $parts[] = $k;
    }
    $n = $i;
    for ($m = 1; $m < $i; $m++) {
        $parts[] = --$n;
    }

    echo $spaces . implode(" ", $parts) . PHP_EOL;
}
?>

How It Works

1. Same shape. Climb and descend logic are unchanged.

2. Pad once. str_repeat(" ", $rows - $i + 1) matches the space-loop count.

3. Join cleanly. implode(" ", $parts) avoids a trailing space after the last number.

Edge Cases & Pitfalls

Check these before calling the solution done.

peak twice

Descending from $i instead of $i − 1

That reprints the peak (1 2 3 3 2 1). Start descent at $n = $i then --$n.

single space

Using " " instead of " "

Single spaces under-pad the pyramid. Keep two spaces per padding step so columns align with k.

PHP_EOL inside

echo PHP_EOL inside a number loop

That splits the palindrome across lines. Call echo PHP_EOL only after all three parts finish.

rows = 1

Single number

Output is a centered 1 — the descend loop does not run.

rows ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

multi-digit

$rows > 9

echo 10 prints two characters and breaks visual centering. Labs usually stick to 1–9.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact implode (Example 3)O(n²)O(1) beyond the row array

There are n rows, and each row prints O(n) spaces and numbers, so total work is O(n²).

Key Takeaways

  • Rule: spaces → 1..$i → $i − 1..1 with --$n.
  • No double peak: descend starts after the climb so the middle value appears once.
  • echo vs PHP_EOL: spaces and numbers stay on the line; echo PHP_EOL advances after all three parts.
  • Next step: Program 57 hollows out the pyramid, printing only the edges.

One line: pad, climb to $i, then descend to 1 for a centered palindrome.

Frequently Asked Questions

Each row reads the same forwards and backwards — e.g. 1 2 3 2 1. Leading spaces center the pyramid shape.
Row 2 prints increasing 1 2, then decreasing 1 using the --$n loop — forming a palindrome.
One loop prints 1..$i, another prints $i-1..1 with --$n. Together they mirror the row without duplicating the peak.
Yes. Use a variable $rows in all loop bounds and space padding.
2*$i - 1 numbers — $i ascending plus $i-1 descending.
Collect numbers in an array and echo implode(' ', $parts) after the leading spaces — see Example 3.
echo stays on the same line for spaces and numbers. echo PHP_EOL ends the row after all three parts finish.
O(n²) for n rows because each row prints O(n) spaces and numbers.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.

Did you know?

Each row is palindromic: print leading spaces, numbers 1..$i, then $i-1..1. Row 3 reads 1 2 3 2 1 when centered.

Next: Hollow Number Pyramid

Continue with a pyramid that prints only the edges.

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