Symmetric Dual-Column Row Number Pattern in PHP

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

What You’ll Learn

How to print a symmetric row-number pattern in PHP: each line shows i twice with a widening gap of spaces between them.

You’ll use two inner loops that walk different column ranges and print a digit only when the column index matches the current row.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
    1
   2 2
  3   3
 4     4
5       5
1

Complete PHP Program

Scan columns from n down to 1, then from 2 up to n; print the column index when it equals the row index, otherwise print a space.

PHP
<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 5; $j >= 1; $j--) {
        if ($i == $j) {
            echo $j;
        } else {
            echo " ";
        }
    }
    for ($k = 2; $k <= 5; $k++) {
        if ($i == $k) {
            echo $k;
        } else {
            echo " ";
        }
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Outer loop is the row

for ($i = 1; $i <= 5; $i++) sets which row number must appear on that line.

Row index
2

Left half: columns high to low

$j runs 5..1. When $i == $j, print $j; else print a space.

Descending scan
3

Right half: columns low to high

$k runs 2..5 with the same rule so the second occurrence of $i lines up on the right.

Ascending scan
4

New line each row

echo PHP_EOL; moves to the next row after both inner loops finish.

Formatting
=

Growing gap between digits

As $i grows, more column positions fall between the two matches, so the space run widens. Work per row is O(n), overall O(n²).

2

Variation — User Input (CLI) Version

This version accepts row count from user input (run from terminal with php):

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

if ($n < 1) {
    echo "Rows must be at least 1" . PHP_EOL;
    exit;
}

for ($i = 1; $i <= $n; $i++) {
    for ($j = $n; $j >= 1; $j--) {
        if ($i == $j) {
            echo $j;
        } else {
            echo " ";
        }
    }
    for ($k = 2; $k <= $n; $k++) {
        if ($i == $k) {
            echo $k;
        } else {
            echo " ";
        }
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Use str_repeat(" ", ...) or str_pad() for wider, even spacing
  • Print stars instead of spaces for a hollow bridge between digits
  • Mirror with three blocks for a wider diamond-style layout
  • Parameterize the right loop start (e.g. skip more middle columns)
  • Collect each row in a string for unit tests

Avoid

  • Starting the second loop at 1 unless you intend to duplicate column 1
  • Mixing HTML &nbsp; in CLI-oriented examples
  • Forgetting to use the same $n in both inner loop bounds
  • Skipping validation when reading row count from input

Key Takeaways

1

Two column scans (descending then ascending) place the same row digit on both sides.

2

Spaces fill every position where the column index does not equal the row index.

3

The right loop starts at 2 so column 1 is handled only once in the left block.

4

The same idea extends to symbols, letters, or multi-character cells with adjusted width.

❓ Frequently Asked Questions

The left block prints 3 when j == 3, and the right block prints 3 when k == 3. All other positions on that row are spaces.
You would print column 1 twice for rows where i == 1, changing the pattern. Starting at 2 matches the intended symmetric layout.
Yes. Assign $n from any trusted source; keep the same inner loop structure.
O(n²) for n rows because each row does about 2n character decisions.

Explore More PHP Number Patterns!

Practice column-based thinking with more symmetric and mirrored layouts.

All Number Patterns →
Did you know?

Treating output as a fixed-width grid (row × column) makes patterns like this easier to debug: print a character for every cell, then adjust rules per cell.

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