Decreasing-Step Number Pattern in PHP

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

What You’ll Learn

How to print a decreasing-step number pattern in PHP like:

1, 2 6, 3 7 10, 4 8 11 13, 5 9 12 14 15.

The key idea is to print the first value of a row, then repeatedly add a step value that decreases by 1 after each print.

⭐ Pattern Output

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

Output
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
1

Complete PHP Program

We set $m to $rows - 1 for each row, then update $k by adding the decreasing step.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    echo $i . " ";
    $m = $rows - 1;
    $k = $i + $m;

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

    echo PHP_EOL;
}

🧠 How It Works

1

Choose the row count

$rows = 5; controls the triangle height.

Setup
2

Print the first number

echo $i; prints the first value of the row (1, 2, 3, 4, 5).

Row start
3

Initialize the decreasing step

$m = $rows - 1; starts at 4, then becomes 3, 2, 1 as we print values.

Step size
4

Update k by adding the step

We compute $k and then repeatedly do $k = $k + $m while $m decreases.

Sequence
=

Decreasing-step rows

The decreasing step size is what creates the distinctive diagonal structure of this pattern.

2

Variation — User Input (CLI) Version

Let the user decide the number of rows at runtime using standard input (run from terminal with php):

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

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

for ($i = 1; $i <= $rows; $i++) {
    echo $i . " ";
    $m = $rows - 1;
    $k = $i + $m;

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

    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Increase $rows to generate a larger triangle
  • Align multi-digit output with printf("%3d ", $value)
  • Store each row in an array and join it to avoid trailing spaces
  • Experiment with different starting steps (not just $rows - 1)
  • Convert it into a matrix by printing a fixed column count

Avoid

  • Resetting $m inside the inner loop incorrectly
  • Forgetting to decrement $m (the pattern will change)
  • Skipping input validation in CLI mode
  • Assuming output stays single-digit when rows get large

Key Takeaways

1

The first value of each row is the row number $i.

2

A step value starts at $rows - 1 and decreases after each print.

3

Updating $k with the decreasing step builds the row sequence.

4

The total printed values for r rows is r(r+1)/2.

❓ Frequently Asked Questions

Starting at $rows - 1 creates the correct spacing between values so the pattern lines up into diagonals as rows increase.
Set $rows = 10. The same logic works because $m is set to $rows - 1 per row.
Yes. Store values in an array and output implode(" ", $row) at the end of each row.
O(r²) for r rows, because total prints are \(1+2+\dots+r\).

Explore More PHP Number Patterns!

Patterns with changing step sizes are great practice for sequence logic and loop thinking.

All Number Patterns →
Did you know?

Many seemingly complex patterns can be generated by tracking a small amount of state (like a step size) and updating it each iteration.

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