Palindrome Number Pattern in PHP

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
1
212
32123
4321234
543212345
1

Complete 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
<?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

1

Choose the row count

$rows = 5; prints 5 lines of the pattern.

Setup
2

Outer loop (rows)

for ($i = 1; $i <= $rows; $i++) controls the current row length.

Row control
3

Print descending (left half)

for ($j = $i; $j > 1; $j--) prints $i, $i-1, ..., 2.

Left half
4

Print ascending (right half)

for ($j = 1; $j <= $i; $j++) prints 1, 2, ..., $i and places 1 in the middle exactly once.

Right half
=

Palindrome-style rows

Each row prints about 2i-1 digits, so total work grows roughly like O(n²).

2

Variation — User Input (CLI) Version

Let the user decide the number of rows at runtime using standard input (run from terminal with php):

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 $rows down to 1)
  • Replace digits with characters to create a similar alphabet palindrome pattern

Avoid

  • Printing 1 twice 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

1

Each row prints a mirrored sequence: descending then ascending.

2

Stop the descending loop at 2 to avoid duplicating 1.

3

Row $i prints 2i-1 digits in total.

4

Total work grows like O(n²) as rows increase.

❓ Frequently Asked Questions

When $i = 3, the first loop prints 3 2, then the second loop prints 1 2 3. Combined, that’s 32123.
Because the second loop already prints 1. If the first loop also printed it, the middle would become 1 1 (duplicated).
Change echo $j; to echo $j . " "; in both inner loops. The pattern stays the same, but spacing changes.
O(n²) for n rows because each row prints O(i) values and the total is 1+2+…+n.

Explore More PHP Number Patterns!

Try more mirrored and pyramid-style number patterns to level up your loop skills.

All Number Patterns →
Did you know?

Each row forms a palindrome around the center 1. That’s why patterns like 4321234 read the same forwards and backwards.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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