Palindrome Number Pattern in PHP

What You’ll Learn
How to print Number Pattern 37 in PHP. Each row prints a mirrored sequence: descending from $i to 2, then ascending from 1 to $i.
This creates a symmetric output like 32123 (often called a palindrome-style number pattern).
⭐ Pattern Output
For $rows = 5, the pattern looks like this:
1
212
32123
4321234
543212345Complete PHP Program
Print the left half (descending) first, then the right half (ascending). We avoid duplicating 1 by stopping the descending loop at 2.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j > 1; $j--) {
echo $j;
}
for ($j = 1; $j <= $i; $j++) {
echo $j;
}
echo PHP_EOL;
}🧠 How It Works
Choose the row count
$rows = 5; prints 5 lines of the pattern.
Outer loop (rows)
for ($i = 1; $i <= $rows; $i++) controls the current row length.
Print descending (left half)
for ($j = $i; $j > 1; $j--) prints $i, $i-1, ..., 2.
Print ascending (right half)
for ($j = 1; $j <= $i; $j++) prints 1, 2, ..., $i and places 1 in the middle exactly once.
Palindrome-style rows
Each row prints about 2i-1 digits, so total work grows roughly like O(n²).
Variation — User Input (CLI) Version
Let the user decide the number of rows at runtime using standard input (run from terminal with php):
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));
if ($rows < 1) {
exit("Rows must be at least 1." . PHP_EOL);
}
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j > 1; $j--) {
echo $j;
}
for ($j = 1; $j <= $i; $j++) {
echo $j;
}
echo PHP_EOL;
}💡 Tips for Enhancement
Try These
- Add spaces between digits for readability (for example, print
$j . " ") - Center-align the output by printing leading spaces before each row
- Generate the same pattern using string concatenation, then echo once per row
- Print the mirrored pattern in reverse order (from
$rowsdown to 1) - Replace digits with characters to create a similar alphabet palindrome pattern
Avoid
- Printing
1twice in the middle (stop the descending loop at 2) - Using invalid row counts without validation in CLI programs
- Mixing responsibilities (outer loop controls rows, inner loops control content)
- Assuming the output will look the same if you add separators (it will change spacing)
Key Takeaways
Each row prints a mirrored sequence: descending then ascending.
Stop the descending loop at 2 to avoid duplicating 1.
Row $i prints 2i-1 digits in total.
Total work grows like O(n²) as rows increase.
❓ Frequently Asked Questions
$i = 3, the first loop prints 3 2, then the second loop prints 1 2 3. Combined, that’s 32123.1. If the first loop also printed it, the middle would become 1 1 (duplicated).echo $j; to echo $j . " "; in both inner loops. The pattern stays the same, but spacing changes.Explore More PHP Number Patterns!
Try more mirrored and pyramid-style number patterns to level up your loop skills.
Each row forms a palindrome around the center 1. That’s why patterns like 4321234 read the same forwards and backwards.
12 people found this page helpful
