PHP Palindrome Number Triangle Pattern (Outer Peak)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A palindrome number triangle prints digits that read the same forward and backward on each row. Build the left half by counting down, then the right half by counting up through 1.
Remember
Rule: print i..2, then 1..i (center is always 1)
1
212
32123
4321234
543212345 ← 5 rows
In PHP use two inner loops per row: $j from $i down to 2, then $j from 1 up to $i. Row $i prints 2i - 1 digits with no spaces between them.
Approach
How to Solve It
One outer loop and two inner loops that mirror around 1.
Method
Idea
Best for
Two-loop palindrome
Descend to 2, then ascend from 1
Learning, interviews, exams
User-input rows
Same logic with a variable height
Practice / demos
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i down to 2:
print j
for j from 1 to i:
print j
print newline
Cheat sheet
Goal
Pattern
Grow the row
for ($i = 1; $i <= $rows; $i++)
Left half
for ($j = $i; $j > 1; $j--) echo $j;
Right half
for ($j = 1; $j <= $i; $j++) echo $j;
End the row
echo PHP_EOL;
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j
Stays on the same line
Each digit (no spaces)
echo PHP_EOL
Ends the current line
After both inner loops
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the palindrome triangle updates instantly — including total digit count.
Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 25 digits
1
212
32123
4321234
543212345
Trace
Worked Walkthrough — $rows = 4
Trace the left descent and the right ascent. The center digit is always 1.
$i
Left / right
Printed row
1
(none) / 1
1
2
2 / 12
212
3
32 / 123
32123
4
432 / 1234
4321234
The left loop stops at 2 so the ascending loop can print the center 1 once — never twice.
Code
PHP Programs
Three complete programs: fixed height, CLI input, and a compact 3-row demo. Use View Output for sample results.
Example 1 — Fixed $rows = 5
Two inner loops per row: descend to 2, then ascend from 1 through $i.