PHP Palindrome Number Pattern (Increasing-Decreasing)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

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.

How to Solve It

Set $m = $i. Print the ascending half, step back with $m = $m - 2, then print the descending half.

MethodIdeaBest for
Two half-loopsAscend with $m++, skip peak, descend with $m--Learning, interviews, demos
Compact traceSame logic with $rows = 3Quick 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

GoalPattern
Start of row$m = $i;
Ascending halffor ($j = 1; $j <= $i; $j++) echo $m++;
Skip peak$m = $m - 2;
Descending halffor ($k = 1; $k < $i; $k++) echo $m--;
Digits on row i2 * $i - 1
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $m++ / echo $m--Stays on the same lineEach digit
echo PHP_EOLEnds the current lineAfter 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.

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 result 5 rows · 25 digits
1
232
34543
4567654
567898765

Worked Walkthrough

Trace row $i = 3 — ascend to the peak, skip it, then descend.

Step$mActionLine so far
Start3$m = $i
Ascend3 → 4 → 5print $m++ three times345
Skip peak6 → 4$m = $m - 2345
Descend4 → 3print $m-- twice34543

Without $m = $m - 2, the peak 5 would print twice. The descending loop runs $i - 1 times so the peak stays unique.

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--.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    $m = $i;

    for ($j = 1; $j <= $i; $j++) {
        echo $m++;
    }

    $m = $m - 2;

    for ($k = 1; $k < $i; $k++) {
        echo $m--;
    }

    echo PHP_EOL;
}
?>

How It Works

1. Seed each row at $i. $m = $i starts the ascending half at the row index.

2. Print up to the peak. The first loop runs $i times with $m++, reaching 2*$i - 1.

3. Skip and mirror. $m = $m - 2 avoids a double peak; the second loop prints $i - 1 digits downward, then echo PHP_EOL ends the row.

Example 2 — User Input Rows

Read the row count at runtime — both halves scale automatically.

PHP
<?php
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));

if (!is_numeric($input) || (int) $input <= 0) {
    echo "Please enter a positive integer." . PHP_EOL;
    exit(1);
}

$rows = (int) $input;

for ($i = 1; $i <= $rows; $i++) {
    $m = $i;

    for ($j = 1; $j <= $i; $j++) {
        echo $m++;
    }

    $m = $m - 2;

    for ($k = 1; $k < $i; $k++) {
        echo $m--;
    }

    echo PHP_EOL;
}
?>

How It Works

1. Same two-loop core. Only the source of $rows changes from a literal to CLI input.

2. Entering 4 stops early. You get four palindromic rows ending at 4567654.

3. Safer input tip. Cap demos for readable output:

Safer input tip
if (!is_numeric($input) || (int) $input < 1 || (int) $input > 9) {
    echo "Enter a whole number from 1 to 9." . PHP_EOL;
    exit(1);
}

Example 3 — Compact $rows = 3

Smaller height for quick tracing of $m++, $m - 2, and $m--.

PHP
<?php
$rows = 3;

for ($i = 1; $i <= $rows; $i++) {
    $m = $i;

    for ($j = 1; $j <= $i; $j++) {
        echo $m++;
    }

    $m = $m - 2;

    for ($k = 1; $k < $i; $k++) {
        echo $m--;
    }

    echo PHP_EOL;
}
?>

How It Works

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.

Time and Space Complexity

ProgramTimeExtra 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.

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²).

Next: Diagonal Mirror Number Pattern

Continue with the next pattern in the PHP number-pattern series.

Program 53 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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