Rotating Number Sequence in PHP

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

What You’ll Learn

How to print a rotating number sequence pattern in PHP using two inner loops per row.

You’ll combine a descending segment and an ascending segment to generate each line.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
12345
21234
32123
43212
54321
1

Complete PHP Program

The first loop prints the left descending part, and the second loop prints the remaining ascending part.

PHP
<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = $i; $j > 1; $j--) {
        echo $j;
    }
    for ($k = 1; $k <= 6 - $i; $k++) {
        echo $k;
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Outer loop controls rows

for ($i = 1; $i <= 5; $i++) prints one line per row.

Row control
2

First inner loop (descending)

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

Left segment
3

Second inner loop (ascending)

for ($k = 1; $k <= 6 - $i; $k++) completes the row with ascending numbers.

Right segment
4

New line

echo PHP_EOL; moves to the next output row.

Line break
=

Shifted sequence rows

Each row prints exactly n digits, so runtime is O(n²) for n rows.

2

Variation — User Input (CLI) Version

This version lets users choose pattern size at runtime (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++) {
    for ($j = $i; $j > 1; $j--) {
        echo $j;
    }
    for ($k = 1; $k <= ($n + 1) - $i; $k++) {
        echo $k;
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits for easier readability
  • Reverse the loops to generate mirrored variants
  • Use letters instead of numbers for alphabet rotation
  • Create a function to reuse the pattern for any n
  • Store rows in an array before rendering

Avoid

  • Hardcoding bounds when you need dynamic size
  • Skipping validation for non-positive inputs
  • Mixing descending/ascending loop bounds incorrectly
  • Forgetting newline after each completed row

Key Takeaways

1

Two inner loops combine descending and ascending segments per row.

2

The pattern gradually rotates from 12345 to 54321.

3

Loop bounds are tightly connected to the row index $i.

4

Same logic style can build many circular/shifted patterns.

❓ Frequently Asked Questions

For $i = 3, first loop prints 3,2 and second loop prints 1,2,3, producing 32123.
Use a variable $n and replace fixed value 5 with $n in all related bounds.
Yes. Append a space in each echo and optionally trim() each row output.
O(n²) since each of n rows prints around n digits.

Explore More PHP Number Patterns!

Practice sequence transformations with more loop-based number designs.

All Number Patterns →
Did you know?

Patterns like this are great for understanding how multiple loops can cooperate to build one line from two different directional sequences.

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