5-Filled Number Pattern in PHP

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:
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5Complete PHP Program
We print numbers from $i to $n, then print $n enough times to keep every row length equal to $n.
<?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
Set the maximum value
$n = 5; controls both the row length and the fill value.
Outer loop (rows)
for ($i = $n; $i >= 1; $i--) sets the starting number of each row (5 down to 1).
First inner loop (ascending sequence)
for ($j = $i; $j <= $n; $j++) prints the left part: $i, $i+1, ... , $n.
Second inner loop (fill with n)
for ($j = 1; $j < $i; $j++) prints $n enough times so every row contains exactly $n numbers.
5-filled rows
Rows gradually reveal more of the ascending sequence while remaining positions stay filled with 5.
Variation — User Input (CLI) Version
Let the user choose the maximum value at runtime (run from terminal with 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 < $imatters for the fill count) - Assuming the fill value is always 5 (parameterize it)
Key Takeaways
The outer loop chooses the row start from $n down to 1.
The first inner loop prints the ascending sequence from $i to $n.
The second inner loop fills remaining slots with the constant value $n.
Parameterizing $n lets you generate the same pattern at different sizes.
❓ Frequently Asked Questions
$i = 5, the ascending loop prints only 5 once, and the fill loop prints four more 5s to complete the row length.$fill = 5 and print $fill in the second inner loop." " strings in the echo statements. With bigger values, spaces make the output easier to read.n values in total.Explore More PHP Number Patterns!
Try combining sequences with fill values to build patterns that look like matrices and pyramids.
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.
12 people found this page helpful
