Symmetric Increasing-Decreasing Pattern in PHP

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

What You’ll Learn

How to print a symmetric increasing-decreasing number pattern in PHP using two inner loops.

Each row starts from the row number, increases to a peak, then mirrors back down.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
1
232
34543
4567654
567898765
1

Complete PHP Program

Use one loop for ascending part and another for descending mirror.

PHP
<?php
for ($i = 1; $i <= 5; $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

Outer loop controls rows

for ($i = 1; $i <= 5; $i++) determines row count and starting value.

Row control
2

Ascending part

Start $m = $i, then print and increment in the first inner loop.

Left side
3

Descending part

Set $m = $m - 2 to avoid peak duplication, then print and decrement in second loop.

Right side
4

Move to next row

echo PHP_EOL; ends each line before the next row starts.

Line break
=

Mirrored rows

Each row length is 2i-1, and total work across rows is O(n²).

2

Variation — User Input (CLI) Version

This version accepts row count from user input (run from terminal with php):

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

if ($n < 1) {
    echo "Rows must be at least 1" . PHP_EOL;
    exit;
}

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

💡 Tips for Enhancement

Try These

  • Add spaces between digits for readability
  • Start from 0 or another base value
  • Use characters instead of numbers for mirrored alpha patterns
  • Pad rows to create centered visual pyramids
  • Wrap logic in a function for reusability

Avoid

  • Forgetting to reduce $m by 2 before descending loop
  • Using incorrect descending loop bound (<= $i)
  • Skipping validation for non-positive row input
  • Mixing HTML-only line breaks in CLI examples

Key Takeaways

1

Each row starts at i and grows upward.

2

Second loop mirrors the row by printing decreasing values.

3

$m = $m - 2 prevents repeating the highest middle value.

4

This approach generalizes to many mirrored number/character patterns.

❓ Frequently Asked Questions

It prints ascending 4,5,6,7 first, then descending 6,5,4 to mirror around the peak.
Change loop limit from 5 to 7 (or use input variable $n).
Yes. Append spaces in each print and trim row end if needed.
O(n²), since total printed characters grow quadratically with rows.

Explore More PHP Number Patterns!

Practice mirror-based loop logic with more progressive number designs.

All Number Patterns →
Did you know?

Patterns that combine ascending and descending segments are a classic way to learn how temporary variables carry state between loops.

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