PHP Palindrome Number Pattern (Increasing-Decreasing)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An increasing-decreasing number pyramid prints each row as a palindrome: count up from $i to the peak 2*$i - 1, then back down to $i. With $rows = 5 you get 1, 232, 34543, 4567654, 567898765.
Remember
Rule: m = i; print up with m++; then m -= 2; print down with m--
1
232
34543
4567654
567898765 ← rows = 5
Unlike Program 51 (one continuous counter across rows), this shape resets $m = $i on every row and builds a self-contained palindrome.
Approach
How to Solve It
Set $m = $i. Print the ascending half, step back with $m = $m - 2, then print the descending half.
Method
Idea
Best for
Two half-loops
Ascend with $m++, skip peak, descend with $m--
Learning, interviews, demos
Compact trace
Same logic with $rows = 3
Quick dry-runs on paper
Pseudocode
Pseudocode
for i from 1 to rows:
m = i
for j from 1 to i:
print m
m = m + 1
m = m - 2
for k from 1 to (i - 1):
print m
m = m - 1
print newline
Cheat sheet
Goal
Pattern
Start of row
$m = $i;
Ascending half
for ($j = 1; $j <= $i; $j++) echo $m++;
Skip peak
$m = $m - 2;
Descending half
for ($k = 1; $k < $i; $k++) echo $m--;
Digits on row i
2 * $i - 1
End the row
echo PHP_EOL;
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $m++ / echo $m--
Stays on the same line
Each digit
echo PHP_EOL
Ends the current line
After both inner loops
Print digits without a newline, then end the row once. Putting echo PHP_EOL inside either half-loop turns the pyramid into a vertical list.
Try it
Live Preview
Change the row count and the palindromic pyramid updates instantly.
Use rows from 1 to 9 (single digits stay tidy). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 25 digits
1
232
34543
4567654
567898765
Trace
Worked Walkthrough
Trace row $i = 3 — ascend to the peak, skip it, then descend.
Step
$m
Action
Line so far
Start
3
$m = $i
Ascend
3 → 4 → 5
print $m++ three times
345
Skip peak
6 → 4
$m = $m - 2
345
Descend
4 → 3
print $m-- twice
34543
Without $m = $m - 2, the peak 5 would print twice. The descending loop runs $i - 1 times so the peak stays unique.
Code
PHP Programs
Three complete programs: fixed $rows = 5, CLI input, and a compact $rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed $rows = 5
Hard-coded height — ascend with $m++, skip the peak, descend with $m--.
1. Only three rows. Easy to dry-run every $m change on paper.
2. Same formula. Nothing changes except $rows — proving the pattern scales.
3. Row 1 has no descent. When $i = 1, the decreasing loop never runs — output is just 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
m - 2
Do not skip the peak step-back
Without $m = $m - 2, the peak digit prints twice and the palindrome breaks.
Descend bound
Use $k < $i, not $k <= $i
The descending half must run $i - 1 times so the peak stays unique.
Newline
Do not put PHP_EOL inside either half-loop
Call echo PHP_EOL only after both halves finish.
Between loops
No break between halves
Run both half-loops back-to-back. A newline between them splits each palindrome in half.
rows = 1
Single value
Output is just 1 — the decreasing loop never runs. A good sanity check.
fgets
Check is_numeric
Bare (int) casts turn non-numeric strings into 0 — prefer is_numeric before casting.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–3)
O(n²)
O(1)
Row $i prints 2*$i - 1 digits, and 1 + 3 + … + (2n - 1) = n² — quadratic in n. Only a few integers of extra memory.
Remember
Key Takeaways
Rule: set $m = $i, print up with $m++, then $m -= 2, then print down with $m--.
Skip the peak:$m = $m - 2 prevents a duplicated middle digit.
Break the row: call echo PHP_EOL only after both half-loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each row $i, print $i..(2*$i-1) then back down to $i, skipping a second peak, then end the line.
Frequently Asked Questions
Row 3 starts at 3, prints up to 5 (345), then prints back down to 3 (43) after $m = $m - 2 — producing 34543.
Each row counts up from $i to the peak 2*$i-1, then counts back down to $i. The sequence reads the same left-to-right on each line.
After the increasing loop, $m is one past the peak. Subtracting 2 moves it to the value just before the peak so the decreasing loop does not repeat the peak digit.
Program 51 uses a continuous counter with alternating direction across rows. Program 52 resets $m = $i each row and builds a palindromic line per row.
Printing a digit stays on the same line. Printing PHP_EOL ends the current row. Digits use echo without a newline; the row break uses echo PHP_EOL after both inner loops.
O(n²) for n rows because row $i prints 2*$i-1 digits and 1+3+5+…+(2n-1) = n² total prints.
Step back with $m = $m - 2 before the decreasing loop. The decreasing loop then runs $i-1 times, skipping the peak.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
🤔
Did you know?
Each row is palindromic: print $i..(2*$i-1) ascending, then back down with $m = $m - 2 to skip the peak. Total digits = 1+3+5+…+(2n-1) = n² — time O(n²).