Home PHP Number Patterns Program 56 PHP Palindromic Number Pyramid Pattern Definition
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.
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.
Approach
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.
Method Idea Best for Fixed rows Hard-code size; three loops per row Labs and demos CLI input Same logic; read $rows at runtime Interactive practice Compact rows implode without trailing spaceCleaner console output
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 Goal Pattern Set size $rows = 5;Leading spaces for ($j = $rows; $j >= $i; $j--) echo " ";Climb for ($k = 1; $k <= $i; $k++) echo $k . " ";Descend $n = $i; for ($m = 1; $m < $i; $m++) echo --$n . " ";End the row echo PHP_EOL;
Printing Numbers vs Starting a New Line API Effect Use for echo " " / echo $k . " "Stays on the same line Spaces and each number in the row echo PHP_EOLEnds the current line After all three parts finish
Build the full centered row with echo, then break once.
Try it
Live Preview Change the row count and the palindromic pyramid updates instantly.
Trace
Worked Walkthrough Trace $rows = 5, row $i = 3 — the three parts that build 1 2 3 2 1.
Part Loop Result Spaces $j = 5..3 → three " "Climb $k = 1..31 2 3Descend $n = 3; --$n twice2 1
Combined: 1 2 3 2 1. The peak 3 appears once — descent starts at $i − 1.
Code
PHP Programs Three complete programs: fixed $rows = 5, CLI input, and compact implode rows. Use View Output for sample results.
1. Fixed rows 2. User input 3. Compact rows Example 1 — Fixed $rows = 5 Hard-coded size — leading spaces, climb 1..$i, descend with --$n.
<?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;
}
?> 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 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
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
$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;
}
?> 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 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 loopThat 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 > 9echo 10 prints two characters and breaks visual centering. Labs usually stick to 1–9.
Analysis
Time and Space Complexity Program Time Extra 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²).
Remember
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 Why is this called a palindromic pyramid? Each row reads the same forwards and backwards — e.g. 1 2 3 2 1. Leading spaces center the pyramid shape.
Why does row 2 look like 1 2 1? Row 2 prints increasing 1 2, then decreasing 1 using the --$n loop — forming a palindrome.
How do the loops create symmetry? One loop prints 1..$i, another prints $i-1..1 with --$n. Together they mirror the row without duplicating the peak.
Can I change 5 to any rows count? Yes. Use a variable $rows in all loop bounds and space padding.
How many numbers are on row i? 2*$i - 1 numbers — $i ascending plus $i-1 descending.
How do I avoid trailing spaces? Collect numbers in an array and echo implode(' ', $parts) after the leading spaces — see Example 3.
What is the difference between echo and PHP_EOL? echo stays on the same line for spaces and numbers. echo PHP_EOL ends the row after all three parts finish.
What is the time complexity? O(n²) for n rows because each row prints O(n) spaces and numbers.
How should I read rows safely? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion