0 and * Mirror Pattern in PHP

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:
*000*000*
0*00*00*0
00*0*0*00
000***000Complete PHP Program
The condition prints * on both diagonals and the center column; otherwise it prints 0.
<?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
Outer loop controls rows
for ($i = 1; $i <= 4; $i++) prints one row per iteration.
Inner loop controls columns
for ($j = 1; $j <= 9; $j++) visits each column position in a row.
Conditional symbol placement
Print * when $i == $j (left diagonal), $j == 5 (center), or $i == 10 - $j (right diagonal). Otherwise print 0.
New line
echo PHP_EOL; moves output to the next row after each inner loop.
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.
Variation — User Input (CLI) Version
This version asks the user for row count and computes width/center dynamically (run from terminal with 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
Use nested loops to traverse rows and columns of a pattern grid.
Conditions decide whether to print * or 0 at each position.
The center-column and mirror-diagonal checks create symmetry.
This idea can be reused for star patterns and alphabet patterns.
❓ Frequently Asked Questions
$j == 5 (or $j == $center in dynamic version), which always prints * at the center column.$i == 10 - $j maps columns from right to left, creating the second diagonal line that mirrors the first.echo "0"; with echo " "; for a cleaner visual style.Explore More PHP Number Patterns!
Practice condition-based pattern logic with more number and symbol layouts.
This pattern combines two diagonals and a center line, similar to drawing an X with an extra middle stroke in a fixed-width grid.
12 people found this page helpful
