Number Pattern 38 in PHP

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

What You’ll Learn

How to print Number Pattern 38 in PHP using nested loops. The first row prints $rows numbers, and each next row prints one fewer number than the previous.

You’ll use a counter variable to keep printing numbers sequentially across rows.

⭐ Pattern Output

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

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

Complete PHP Program

The inner loop counts down from $rows to the current row index $i, so each next row prints one fewer number.

PHP
<?php
$rows = 5;
$k = 1;

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

🧠 How It Works

1

Initialize rows and counter

$rows = 5; sets the triangle size and $k = 1; starts the sequential numbering.

Setup
2

Outer loop (rows)

for ($i = 1; $i <= $rows; $i++) moves from row 1 to row $rows.

Row control
3

Inner loop (shrinking width)

for ($j = $rows; $j >= $i; $j--) prints $rows - $i + 1 numbers on row $i.

Width control
4

Print and increment

echo $k++; prints the current counter value and increments it for the next position.

Sequential numbers
=

Shrinking number triangle

Total prints are n(n+1)/2, so runtime is O(n²) for n rows.

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) {
    exit("Rows must be at least 1." . PHP_EOL);
}

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

💡 Tips for Enhancement

Try These

  • Use str_pad() to align multi-digit columns (10, 11, 12, ...)
  • Start from a different initial value by setting $k accordingly
  • Print commas instead of spaces for CSV-style output
  • Reverse the structure (growing triangle) by changing the inner-loop bounds
  • Store each row in an array for later processing

Avoid

  • Forgetting to print a newline after each row
  • Using $rows less than 1 without validation
  • Mixing row and column responsibilities (outer loop = rows)
  • Assuming spacing stays aligned when values become multi-digit

Key Takeaways

1

A counter prints sequential values across all rows.

2

Each next row prints one fewer number than the previous.

3

Total prints are n(n+1)/2 for n rows.

4

Runtime grows as O(n²).

❓ Frequently Asked Questions

Because on the last row $i = $rows, the inner loop runs only once (from $rows down to $rows).
Build the row as a string and trim() it before echoing, or print spaces only between values.
Yes. Change the inner loop to run from 1 to $i so each row prints one more value than the previous.
O(n²) for n rows because total prints are n(n+1)/2.

Explore More PHP Number Patterns!

Practice counters and loop bounds by trying more shrinking and growing patterns.

All Number Patterns →
Did you know?

The total count of printed numbers forms a triangular number. For $rows = 5, that’s 15 numbers (1 through 15).

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