0 and * Mirror Pattern in PHP

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

What You’ll Learn

How to print a 0 and * mirror-style number pattern in PHP using nested for loops and an if condition.

You will learn how row and column indexes can be used to place symbols on diagonals and at the center column while filling other positions with 0.

⭐ Pattern Output

For 4 rows and 9 columns, the pattern looks like this:

Output
*000*000*
0*00*00*0
00*0*0*00
000***000
1

Complete PHP Program

The condition prints * on both diagonals and the center column; otherwise it prints 0.

PHP
<?php
for ($i = 1; $i <= 4; $i++) {
    for ($j = 1; $j <= 9; $j++) {
        if ($i == $j || $j == 5 || $i == 10 - $j) {
            echo "*";
        } else {
            echo "0";
        }
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Outer loop controls rows

for ($i = 1; $i <= 4; $i++) prints one row per iteration.

Row control
2

Inner loop controls columns

for ($j = 1; $j <= 9; $j++) visits each column position in a row.

Column control
3

Conditional symbol placement

Print * when $i == $j (left diagonal), $j == 5 (center), or $i == 10 - $j (right diagonal). Otherwise print 0.

Pattern logic
4

New line

echo PHP_EOL; moves output to the next row after each inner loop.

Line break
=

Symmetric 0/* pattern

The program checks every cell in a 4x9 grid, so complexity is O(r*c) where r is rows and c is columns.

2

Variation — User Input (CLI) Version

This version asks the user for row count and computes width/center dynamically (run from terminal with php):

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

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

$width = 2 * $rows + 1;
$center = $rows + 1;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = 1; $j <= $width; $j++) {
        if ($i == $j || $j == $center || $i == ($width + 1) - $j) {
            echo "*";
        } else {
            echo "0";
        }
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Generalize rows/columns instead of hardcoding 4 and 9
  • Use different fill characters instead of 0
  • Store each row in a string, then print once per line
  • Add input validation for non-numeric user input
  • Create an inverted version by reversing row direction

Avoid

  • Mixing row and column variables inside conditions
  • Forgetting to update mirror formula when width changes
  • Skipping the newline after each row
  • Assuming CLI input is always valid

Key Takeaways

1

Use nested loops to traverse rows and columns of a pattern grid.

2

Conditions decide whether to print * or 0 at each position.

3

The center-column and mirror-diagonal checks create symmetry.

4

This idea can be reused for star patterns and alphabet patterns.

❓ Frequently Asked Questions

Because the condition includes $j == 5 (or $j == $center in dynamic version), which always prints * at the center column.
The check $i == 10 - $j maps columns from right to left, creating the second diagonal line that mirrors the first.
Yes. Replace echo "0"; with echo " "; for a cleaner visual style.
O(r*c), because each cell is visited once by the nested loops.

Explore More PHP Number Patterns!

Practice condition-based pattern logic with more number and symbol layouts.

All Number Patterns →
Did you know?

This pattern combines two diagonals and a center line, similar to drawing an X with an extra middle stroke in a fixed-width grid.

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