5-Filled Number Pattern in PHP

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

What You’ll Learn

How to print a 5-filled number pattern in PHP. Each row ends with 5, and the left side grows as an ascending sequence: 4 5, 3 4 5, ... up to 1 2 3 4 5.

We’ll use nested loops: one prints the ascending part, and another fills the remaining slots with 5.

⭐ Pattern Output

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

Output
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5
1

Complete PHP Program

We print numbers from $i to $n, then print $n enough times to keep every row length equal to $n.

PHP
<?php
$n = 5;

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

🧠 How It Works

1

Set the maximum value

$n = 5; controls both the row length and the fill value.

Setup
2

Outer loop (rows)

for ($i = $n; $i >= 1; $i--) sets the starting number of each row (5 down to 1).

Row start
3

First inner loop (ascending sequence)

for ($j = $i; $j <= $n; $j++) prints the left part: $i, $i+1, ... , $n.

Ascending
4

Second inner loop (fill with n)

for ($j = 1; $j < $i; $j++) prints $n enough times so every row contains exactly $n numbers.

Filling
=

5-filled rows

Rows gradually reveal more of the ascending sequence while remaining positions stay filled with 5.

2

Variation — User Input (CLI) Version

Let the user choose the maximum value at runtime (run from terminal with php):

PHP
<?php
echo "Enter the maximum value (e.g., 5): ";
$n = (int) trim(fgets(STDIN));

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

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

💡 Tips for Enhancement

Try These

  • Use a different fill value (like 9) to create new variants
  • Print without trailing spaces by joining an array of values
  • Right-align the pattern using leading spaces
  • Turn it into a square grid by removing the decreasing outer loop
  • Replace numbers with characters to create alphabet patterns

Avoid

  • Forgetting to reset loop variables each row
  • Skipping input validation in CLI mode
  • Mixing up the bounds ($j < $i matters for the fill count)
  • Assuming the fill value is always 5 (parameterize it)

Key Takeaways

1

The outer loop chooses the row start from $n down to 1.

2

The first inner loop prints the ascending sequence from $i to $n.

3

The second inner loop fills remaining slots with the constant value $n.

4

Parameterizing $n lets you generate the same pattern at different sizes.

❓ Frequently Asked Questions

When $i = 5, the ascending loop prints only 5 once, and the fill loop prints four more 5s to complete the row length.
Yes. Use a separate variable like $fill = 5 and print $fill in the second inner loop.
Remove the " " strings in the echo statements. With bigger values, spaces make the output easier to read.
O(n²) for n columns/rows, because each row prints n values in total.

Explore More PHP Number Patterns!

Try combining sequences with fill values to build patterns that look like matrices and pyramids.

All Number Patterns →
Did you know?

Patterns like this are a simple way to practice thinking in rows and columns, which is the same mental model used for 2D arrays and matrices.

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