Alternating Ascending/Descending Number Pattern in PHP

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

What You’ll Learn

How to print an alternating ascending/descending number pattern in PHP. Odd-length rows print numbers from 1 to $i, and even-length rows print from $i down to 1.

This pattern is great for practicing nested loops along with conditional logic using if/else.

⭐ Pattern Output

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

Output
12345
4321
123
21
1
1

Complete PHP Program

Fixed $n = 5 version:

PHP
<?php
$n = 5;

for ($i = $n; $i >= 1; $i--) {
    if ($i % 2 === 0) {
        for ($j = $i; $j >= 1; $j--) {
            echo $j;
        }
    } else {
        for ($j = 1; $j <= $i; $j++) {
            echo $j;
        }
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Choose the size

$n = 5; sets the largest row length.

Setup
2

Outer loop (row length decreases)

for ($i = $n; $i >= 1; $i--) controls the current row length.

Row control
3

If/else decides direction

When $i is even, print descending with for ($j = $i; $j >= 1; $j--). When $i is odd, print ascending with for ($j = 1; $j <= $i; $j++).

Direction
4

New line

echo PHP_EOL; ends each row.

Line break
=

Alternating direction pattern

You still print about n(n+1)/2 digits total, so runtime is O(n²).

2

Variation — User Input (CLI) Version

Let the user decide $n at runtime (run from terminal with php):

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

for ($i = $n; $i >= 1; $i--) {
    if ($i % 2 === 0) {
        for ($j = $i; $j >= 1; $j--) {
            echo $j;
        }
    } else {
        for ($j = 1; $j <= $i; $j++) {
            echo $j;
        }
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Validate input (reject $n < 1) before printing
  • Add spaces between numbers for readability
  • Alternate based on row index rather than $i parity
  • Invert the condition to swap which rows print ascending/descending
  • Try alternating two different patterns (numbers vs stars)

Avoid

  • Forgetting the newline after each row
  • Using negative or zero values without validation
  • Mixing up loop directions inside the if/else
  • Hard-coding 5 instead of using $n

Key Takeaways

1

if/else can switch between two different inner-loop directions.

2

Odd rows can print ascending (1..i) while even rows print descending (i..1).

3

Total prints stay triangular, so runtime is O(n²).

4

This pattern is a stepping stone for zig-zag and wave-style outputs.

❓ Frequently Asked Questions

Because the second row length is 4 (even), so the code enters the even branch and prints from 4 down to 1.
Set $n to the maximum row length you want. The program prints rows from $n down to 1.
Yes. Track a separate counter like $row and alternate when $row % 2 === 0.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Next: PHP Number Pattern 14

Continue to Program 14 for the next number pattern exercise in PHP.

Program 14 →
Did you know?

Adding a single if can dramatically increase the variety of patterns you can generate—great for practicing clean control flow.

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