Leading Spaces
$j = $rows..$i
Echo $rows - $i + 1 pairs of spaces to center each row.

The palindromic number pyramid prints centered rows that read the same forwards and backwards. For $rows = 5: 1, 1 2 1, 1 2 3 2 1, and so on. Each row uses leading spaces, an increase loop 1..$i, and a decrease loop $i-1..1. This tutorial covers the three-part row logic, live preview, worked PHP examples, edge cases, and O(n²) complexity.
$j = $rows..$i
Echo $rows - $i + 1 pairs of spaces to center each row.
$k = 1..$i
Echo numbers 1 through $i on the way up.
--$n
Set $n = $i, then echo --$n for $i-1 steps — mirrors without duplicating peak.
2i - 1 nums
Row i has 2i-1 numbers — increase half plus decrease half.
3–12 rows
Pick row count and draw the palindromic pyramid instantly in the browser.
Complexity
Each row prints O(n) spaces and numbers — total work grows as n².
A palindromic number pyramid centers each row with leading spaces, then prints 1..i and i-1..1. Row 2 reads 1 2 1; row 3 reads 1 2 3 2 1.
In PHP: outer for ($i = 1; $i <= $rows; $i++), space loop $j = $rows..$i, increase loop $k = 1..$i, decrease loop with --$n, then echo PHP_EOL.
Given $rows = 5, print five centered palindromic rows — widest row has 1 2 3 4 5 4 3 2 1.
// $rows = 5 (conceptual output — centered)
// 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 | Item | Type | Description |
|---|---|---|
$rows | int | Number of triangle rows (typically ≥ 1). |
i, j, k, n, m | int | Row i; space index j; increase k; decrease n/m. |
| Printed output | text | 2i-1 numbers per row when centered; palindromic sequence. |
for $i from 1 to $rows:
echo ($rows-$i+1) space pairs
echo 1..$i
echo $i-1..1 using --$n
echo PHP_EOL | Approach | Idea | Best for |
|---|---|---|
| Three-part row | Spaces + increase loop + decrease loop | Centered palindromic rows |
| implode row | Build row without trailing spaces | Cleaner console output — Example 3 |
| fgets(STDIN) input | (int) $input for rows | User-chosen row count |
| Compact output | implode joins values with single spaces | No trailing space per row — Example 3 |
| Goal | Pattern |
|---|---|
| Set rows | $rows = 5; |
| Outer loop | for ($i = 1; $i <= $rows; $i++) |
| Space loop | for ($j = $rows; $j >= $i; $j--) echo " " |
| Increase loop | for ($k = 1; $k <= $i; $k++) echo $k |
| Decrease loop | $n = $i; echo --$n for $m = 1..$i-1 |
| Row break | echo PHP_EOL; after all three parts |
| Program 55 contrast | Diagonal-fill triangle uses step logic; this pyramid uses centered palindrome rows |
How leading spaces, increase loop, decrease loop, and row breaks work together.
for ($j = $rows; $j >= $i; $j--)
echo " "Centers row $i in the pyramid.
for ($k = 1; $k <= $i; $k++)
echo $k . " "Echoes 1 2 3 ... $i on row $i.
$n = $i
for ($m = 1; $m < $i; $m++)
echo --$nEchoes $i-1 ... 1 — mirrors without repeating peak.
trace $i=3, $rows=5Dry-run row 3: spaces + 1 2 3 + 2 1 → palindrome.
Reach for this pattern when teaching centering, palindrome rows, and nested loops.
Classic follow-up after diagonal-fill triangles — introduces centered palindromic rows.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible pattern size.
Compare with Program 55 (diagonal-fill triangle), then continue to Program 57 hollow pyramid.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one program that locks in centering, palindrome rows, and O(n²) thinking.
Choose pattern size n and draw the full palindromic number pyramid number pattern in the browser.
Three complete PHP programs — fixed $rows = 5, fgets(STDIN) input, and a compact implode variant. Click View Output to reveal sample console results.
Print five centered palindromic rows — widest row has nine numbers.
$rows = 5Hard-coded $rows = 5 — leading spaces, increase loop 1..$i, decrease loop 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;
} Row 1 prints one centered 1. Row 3 prints spaces, then 1 2 3, then 2 1 — a palindrome.
Read $rows with fgets(STDIN) for flexible output size.
Same palindromic row logic; row count comes from user input.
<?php
echo "Enter rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . 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;
} Same palindromic row logic as Example 1; row count comes from fgets(STDIN) input.
Build each row with an array and implode — no trailing space.
Same logic; use an array and implode to join values 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;
} Same palindromic logic; implode on an array produces clean rows without trailing spaces.
Set $rows (e.g. 5). Outer loop for ($i = 1; $i <= $rows; $i++) builds one centered row per iteration.
for ($j = $rows; $j >= $i; $j--) echoes " " to center row $i.
for ($k = 1; $k <= $i; $k++) echoes 1 2 3 ... $i.
$n = $i, then echo --$n for $m = 1..$i-1; then echo PHP_EOL.
Total numbers = n² (e.g. 25 for $rows=5) — O(n²) time, O(1) extra memory.
$i = 3, $rows = 5Trace row 3 to see spaces, increase half, and decrease half form 1 2 3 2 1.
| Phase | Loop | Row so far |
|---|---|---|
| Spaces | j=5..3 | |
| Increase | k=1..3 | 1 2 3 |
| Decrease | m=1,2 → --n | 1 2 3 2 1 |
Final centered row 3: 1 2 3 2 1. Then echo PHP_EOL moves to row 4.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: use fgets(STDIN) for dynamic rows — see Example 2.
Each row echoes $rows - $i + 1 pairs of spaces before numbers.
Example: trace row 3 with rows=5 — 2 space pairs before digits.
Practice echo PHP_EOL vs echo for multi-line vs same-line output.
Example: use implode for clean rows — see Example 3.
Each row reads the same forwards and backwards — peak digit appears once.
Example: row 4 reads 1 2 3 4 3 2 1 when centered.
Each row prints O(n) spaces and numbers — total work grows as n².
Example: count numbers on row 5 → 2×5-1 = 9 digits per row.
Pair the pattern with fgets(STDIN) and positive-row checks.
Example: reject $rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner loop roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner PHP courses.
Missing decrease loop shows immediately — rows are not palindromic.
Only loops and console output — no arrays or math libraries.
Change rows, use fgets(STDIN), or build rows with implode.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the three-part row (spaces, up, down) first; then try fgets(STDIN) input and the implode variant in Example 3.
Small habits that keep number-pattern code clean.
Use $rows for height and $i/$j/$k/$n for loop variables.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Finish the inner loops for row $i, then call echo PHP_EOL.
Set $n = $i before the decrease loop; echo --$n exactly $i-1 times.
Trace $rows = 5, $i = 3 on paper — expect centered 1 2 3 2 1.
Pro Tip: if the peak digit prints twice, check whether the decrease loop uses $m < $i.
Mistakes that commonly break palindromic number pyramid number patterns.
Each number lands on its own line — you get a vertical stack, not a centered pyramid row.
→ Use echo inside space, increase, and decrease loops; echo PHP_EOL only after they finish.
Without --$n, rows echo only 1..$i — no palindrome.
→ Add the decrease loop: $n = $i, then echo --$n for $m = 1..$i-1.
Omitting echo PHP_EOL after the three inner loops glues all rows onto one line.
→ Always call echo PHP_EOL after space, increase, and decrease loops complete.
Looping m = 1..i duplicates the peak digit — row 3 becomes 1 2 3 3 2 1.
→ Use m < i so exactly i-1 descending digits print.
Letters or empty input throw invalid input without validation.
→ Use is_numeric($input) before (int) $input.
Using literal 5 in loop bounds instead of variable $rows breaks dynamic input.
→ Use one $rows variable for outer loop and space/number bounds.
Check these inputs before calling the solution done.
Output is one centered line: 1 — the decrease loop does not run.
Loop never runs — print nothing or show a message.
$rows < 0Treat as invalid; re-prompt instead of silent empty output.
Large values produce wide rows — fine for labs; use smaller n for quick demos.
Unchecked fgets(STDIN) input leaves $rows unset — call is_numeric($input) first.
Use conditional spacing to avoid trailing spaces — see Example 3.
Try these variations to lock in the pattern.
$rows = 3, 6, or 8i reads the same forwards and backwards--n loop and see non-palindromic rows$i has 2*$i-1 numbers; total = n² (e.g. 25 numbers for $rows=5).echo stays on the line; echo PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 prints one number.1..i, then --n for i-1 steps.Quick Takeaway: outer $i=1..$rows; spaces; echo 1..$i; echo --$n; then echo PHP_EOL.
| Program | Time | Extra space |
|---|---|---|
Fixed $rows = 5 (Example 1) | O(n²) | O(1) |
| fgets(STDIN) input (Example 2) | O(n²) | O(1) |
| Compact rows (Example 3) | O(n²) | O(1) |
The palindromic number pyramid combines centering with increase/decrease loops — a natural step after diagonal-fill triangle patterns. Master the fixed-$rows version first, then try fgets(STDIN) input and the compact implode variant in Example 3.
Practice the three examples above, then continue to Program 57 for the hollow number pyramid pattern.
Print leading spaces before numbers on every row — one echo PHP_EOL per outer iteration.
i, space loop, increase loop, and decrease loop before coding1..$i, then --$n; then echo PHP_EOL$rows ≥ 1 for interactive programsfgets(STDIN) return value before using $rowsm <= i in decrease loop (duplicates peak)$rows$rows = 1 edge caseCenter each row with spaces, then echo increase and decrease halves.
Row $i has 2*$i-1 numbers
$i = 1..$rows
CodeEcho 1..$i then --$n
Logic2*$i-1 nums/row
O(n²)
AnalysisEach row is palindromic: print leading spaces, numbers 1..$i, then $i-1..1. Row 3 reads 1 2 3 2 1 when centered.
Move on to the hollow number pyramid pattern in the PHP number-pattern series.
12 people found this page helpful