Odd-Step Descending Number Pattern in PHP

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

What You’ll Learn

How to print an odd-step descending number pattern in PHP using nested for loops. The first row prints 1 through a starting width (like 7), then each next row prints two fewer numbers.

This pattern is useful for practicing how the outer loop controls row length and how the inner loop controls what appears in each row.

⭐ Pattern Output

For a starting width of 7, the pattern looks like this:

Output
1234567
12345
123
1
1

Complete PHP Program

The outer loop reduces the row width by 2. The inner loop prints numbers from 1 to the current row width.

PHP
<?php
$start = 7;

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

🧠 How It Works

1

Pick the starting width

$start = 7; sets the first row length.

Setup
2

Outer loop (shrink by 2)

for ($i = $start; $i >= 1; $i -= 2) produces widths 7, 5, 3, 1 by subtracting 2 each row.

Row control
3

Inner loop (print 1..$i)

for ($j = 1; $j <= $i; $j++) prints digits from 1 up to the current width $i using echo $j;.

Number printing
4

Move to the next line

echo PHP_EOL; prints a newline after each row.

Line break
=

Odd-step descending rows

Row lengths are odd numbers that decrease by 2: 7, 5, 3, 1. This is a compact way to practice nested loops and step sizes.

2

Variation — User Input (CLI) Version

Let the user choose the starting width at runtime using standard input (run from terminal with php):

PHP
<?php
echo "Enter the starting width (odd number): ";
$start = (int) trim(fgets(STDIN));

if ($start < 1) {
    echo "Please enter a positive number." . PHP_EOL;
    exit(1);
}

if ($start % 2 === 0) {
    $start -= 1; // make it odd for a clean  ... 5,3,1 ending
}

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

💡 Tips for Enhancement

Try These

  • Change $start to print larger or smaller patterns (9, 11, ...)
  • Add spaces between numbers (for example, echo $j . " ";)
  • Right-align the pattern by printing leading spaces on each row
  • Replace numbers with characters to create alphabet patterns
  • Validate input to ensure $start is positive

Avoid

  • Using an even start without adjusting (you’ll end with ... 2 instead of ... 1)
  • Forgetting the newline after each row
  • Mixing up row/column logic (keep the outer loop for row widths)
  • Assuming user input is always valid in CLI mode

Key Takeaways

1

The outer loop shrinks the row length by 2 each time.

2

The inner loop prints numbers from 1 to the current width $i.

3

Odd widths (7, 5, 3, 1) create a clean descending pattern.

4

The same approach extends to stars and letters by changing what you print.

❓ Frequently Asked Questions

Because the outer loop decreases by 2 each time ($i -= 2), so the number of printed digits shrinks by two per row.
Yes—set $start to any positive odd number. The rows will be $start, $start-2, $start-4, ... down to 1.
Change the inner loop to echo $j . " ";. That makes the output easier to read for bigger widths.
O(n²) with n as the starting width. You print n + (n-2) + (n-4) + ... + 1 digits overall.

Explore More PHP Number Patterns!

Practice nested loops with patterns that change width, steps, alignment, and symmetry.

All Number Patterns →
Did you know?

If you start from an odd width n, the total numbers printed are n + (n-2) + (n-4) + ... + 1, which equals \(((n+1)/2)^2\). For n = 7, that’s 16 digits.

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