Palindrome Number Triangle in PHP

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

What You’ll Learn

How to print a palindrome number triangle in PHP:

1, 121, 12321, 1234321, 123454321.

The idea is simple: print numbers increasing from 1 to $i, then print them back down from $i-1 to 1.

⭐ Pattern Output

For $rows = 5, the pattern looks like this:

Output
1
121
12321
1234321
123454321
1

Complete PHP Program

First we print 1..$i, then we print $i-1..1 to mirror the row.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $j;
    }
    for ($k = $i - 1; $k >= 1; $k--) {
        echo $k;
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Choose the row count

$rows = 5; sets how many palindrome rows to print.

Setup
2

Outer loop (rows)

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

Row control
3

Left side: print 1..i

The first inner loop prints 1 through $i.

Ascending
4

Right side: print i-1..1

The second inner loop prints back down from $i-1 to 1, completing the palindrome.

Mirroring
=

Palindrome rows

Each row is symmetric because the right side is the reverse of the left side (without repeating the peak).

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) {
    echo "Please enter a positive number." . PHP_EOL;
    exit(1);
}

for ($i = 1; $i <= $rows; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $j;
    }
    for ($k = $i - 1; $k >= 1; $k--) {
        echo $k;
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits for readability when $rows is large
  • Center-align the triangle by printing leading spaces before each row
  • Print the descending version by iterating $i from $rows down to 1
  • Use printf padding to align multi-digit numbers
  • Swap digits for characters to build alphabet palindromes

Avoid

  • Starting the mirror loop at $i (it will repeat the peak number)
  • Forgetting the newline after each row
  • Mixing row/column logic in nested loops
  • Skipping input validation in CLI mode

Key Takeaways

1

Print 1..i to build the left half of the row.

2

Print i-1..1 to mirror without duplicating the peak.

3

The outer loop controls the peak digit and the number of rows.

4

This pattern is a classic example of building symmetric output with loops.

❓ Frequently Asked Questions

Because we start the mirror loop at $i - 1. That prints the reverse without duplicating the peak digit.
Print leading spaces before the first loop. The number of spaces should decrease as $i increases.
Yes—change the starting value for each row and adjust what you print inside the loops.
O(n²) for n rows, because total prints grow quadratically.

Explore More PHP Number Patterns!

Palindrome patterns are a great stepping stone to diamonds and centered pyramids.

All Number Patterns →
Did you know?

Palindromes show up everywhere in coding interviews—this pattern is a visual way to practice the same mirroring idea.

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