Main Diagonal
$i == $j
Left loop prints digit $j when row $i equals column $j.

The diagonal mirror pattern prints numbers on the main diagonal and mirror diagonal with spaces elsewhere — forming an X shape. For $n = 5: 1 1, 2 2, 3 3. This tutorial covers $i==$j and $i==$k logic, live preview, worked PHP examples, edge cases, and O(n²) complexity.
$i == $j
Left loop prints digit $j when row $i equals column $j.
$i == $k
Right loop prints digit $k when $i == $k; runs $k = $n-1..1.
else " "
All non-diagonal positions print a space — only two digits per row (except overlap).
2*$n-1 cells
Each row has exactly 2*$n-1 character positions for size $n.
3–12 for n
Pick a size n and draw the diagonal mirror pattern instantly in the browser.
Complexity
Each row checks 2*$n-1 cells — total work grows as n².
A diagonal mirror number pattern places digits only on the main diagonal ($i == $j) and mirror diagonal ($i == $k). Row 1 shows 1 1; row 3 shows digits at both X arms.
In PHP: outer for ($i = 1; $i <= $n; $i++), inner for ($j = 1; $j <= $n; $j++) echo digit or space, inner for ($k = $n-1; $k >= 1; $k--) echo digit or space, then echo PHP_EOL after both halves.
It is a nested-loop exercise that places digits only on the main and mirror diagonals.
$i = 1..$n picks each row number.
Left loop echoes when $i==$j; mirror loop echoes when $i==$k.
Two loops per row — main diagonal half, then mirror diagonal half.
Follow Program 52 palindrome rows; continue to Program 54 diamond diagonal.
In short: outer $i=1..$n, inner $j=1..$n with $i==$j, inner $k=$n-1..1 with $i==$k, then echo PHP_EOL.
Given $n = 5, print five lines with digits on both diagonals and spaces elsewhere.
// $n = 5 (conceptual output)
// 1 1
// 2 2
// 3 3
// 4 4
// 5 | Item | Type | Description |
|---|---|---|
$n | int | Pattern size — number of rows and diagonal width (typically ≥ 1). |
$i, $j, $k | int | Row index $i; column $j for main diagonal, column $k for mirror diagonal. |
| Printed output | text | 2*$n-1 cells per row — fixed width for size $n. |
for i from 1 to n:
for j from 1 to n: print j if $i==$j else space
for k from n-1 down to 1: print k if $i==$k else space
newline | Approach | Idea | Best for |
|---|---|---|
| Two diagonal loops | if ($i==$j) echo $j else echo " "; mirror loop with $i==$k | Fixed width — 2*$n-1 cells per row |
| fgets(STDIN) input | (int) $input after is_numeric($input) | User-chosen pattern size |
| Star diagonals | Print * on diagonals instead of numbers | Visual X-shape without digits — Example 3 |
| Goal | Pattern |
|---|---|
| Set size | $n = 5; |
| Outer loop | for ($i = 1; $i <= $n; $i++) |
| Main diagonal | for ($j = 1; $j <= $n; $j++) — echo when $i == $j |
| Mirror diagonal | for ($k = $n-1; $k >= 1; $k--) — echo when $i == $k |
| Non-diagonal cell | echo " "); |
| Row break | echo PHP_EOL; after both halves |
| Program 52 contrast | Palindrome rows use increase/decrease loops; this pattern uses $i==$j and $i==$k diagonal checks |
How outer row selection, main diagonal loop, mirror diagonal loop, and row breaks work together.
for ($i = 1; $i <= $n; $i++)Picks row number $i — also the diagonal digit value.
for ($j = 1; $j <= $n; $j++)
if ($i == $j) echo $j
else echo " "Prints digit at column $j when $i == $j.
for ($k = $n-1; $k >= 1; $k--)
if ($i == $k) echo $k
else echo " "Prints digit when $i == $k; loop starts at $n-1.
trace i=3, n=5Dry-run row 3 — digits at both diagonal positions: 3 3.
Reach for this pattern when teaching nested loops, diagonal conditions, and fixed-width row output.
Classic follow-up after palindrome row patterns like Program 52.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible pattern size.
Compare with Program 52 (palindrome rows), then continue to Program 54 (diamond diagonal).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in $i==$j, $i==$k, mirror bounds, and O(n²) thinking.
Choose pattern size $n and draw the diagonal mirror number pattern in the browser.
Three complete PHP programs — fixed $n = 5, fgets(STDIN) input, and a star-diagonal variant. Click View Output to reveal sample console results.
Print five rows with digits on main and mirror diagonals — X-shaped output.
$n = 5Hard-coded size — left loop with $i==$j, mirror loop with $i==$k.
<?php
$n = 5;
for ($i = 1; $i <= $n; $i++) {
for ($j = 1; $j <= $n; $j++) {
if ($i == $j) echo $j;
else echo " ";
}
for ($k = $n - 1; $k >= 1; $k--) {
if ($i == $k) echo $k;
else echo " ";
}
echo PHP_EOL;
} When $i = 3 and $n = 5, the main diagonal prints 3 at column 3; the mirror diagonal also prints 3. Row 1 prints 1 on both diagonals — 1 1.
Read $n with fgets(STDIN) for flexible output size.
Same diagonal conditions; size n comes from user input.
<?php
echo "Enter n: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . PHP_EOL;
exit(1);
}
$n = (int) $input;
for ($i = 1; $i <= $n; $i++) {
for ($j = 1; $j <= $n; $j++) {
echo $i == $j ? $j : " ";
}
for ($k = $n - 1; $k >= 1; $k--) {
echo $i == $k ? $k : " ";
}
echo PHP_EOL;
} Identical diagonal logic to Example 1; only the size is dynamic.
Print * on diagonals instead of row numbers.
Replace digits with * when $i==$j or $i==$k.
<?php
$n = 5;
for ($i = 1; $i <= $n; $i++) {
for ($j = 1; $j <= $n; $j++) {
echo $i == $j ? "*" : " ";
}
for ($k = $n - 1; $k >= 1; $k--) {
echo $i == $k ? "*" : " ";
}
echo PHP_EOL;
} Same diagonal logic; only the printed character changes from digit to *.
echo is built in; use fgets(STDIN) when reading input. Set $n (e.g. 5).
for ($i = 1; $i <= $n; $i++) — one iteration per output line.
Left loop: echo digit when $i==$j, else space. Mirror loop: echo when $i==$k for $k = $n-1..1.
echo PHP_EOL; after both inner loops moves to the next row.
Total checks = n(2n-1) — O(n²) time, O(1) extra memory.
$i = 3, $n = 5Trace row 3 to see how main and mirror diagonal checks place digits at both X arms.
| Half | Condition hit | Row so far |
|---|---|---|
| Left (j=1..2) | no match | |
| Left ($j=3) | $i==$j → echo 3 | 3 |
| Left (j=4,5) | spaces | 3 |
| Mirror (k=4,5) | spaces | 3 |
| Mirror ($k=3) | $i==$k → echo 3 | 3 3 |
| Mirror (k=2,1) | spaces | 3 3 |
Final row: 3 3. Then echo PHP_EOL moves to the next row.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: use fgets(STDIN) for dynamic n — see Example 2.
Mirror loop runs k = n-1 down to 1 so the center column is not duplicated.
Example: compare row 3 with n=5 — digits at columns 3 and 3 on main and mirror diagonals.
Practice echo vs PHP_EOL for multi-line vs single-line output.
Example: use echo "*" when $i==$j or $i==$k — see Example 3.
Print * when $i==$j or $i==$k instead of the row number.
Example: print $n=5 with * on diagonals and compare the X shape.
Fixed width 2n-1 makes O(n²) concrete for beginners.
Example: count cells for n=5 → 5 rows × 9 cells = 45 prints.
Pair the pattern with fgets(STDIN) and positive-row checks.
Example: reject n <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner loop roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner PHP courses.
Wrong mirror loop start shows immediately — center column may print twice.
Only loops and console output — no arrays or math libraries.
Change $n, use fgets(STDIN), or print * on diagonals instead of numbers.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the $i==$j and $i==$k conditions first; then try fgets(STDIN) input and the star variant in Example 3.
Small habits that keep number-pattern code clean.
Use $n for pattern size and $i/$j/$k for row/column indices.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Run left diagonal loop, run mirror loop, then echo PHP_EOL.
Use echo $j or echo " " in the main loop; same for the mirror loop; one echo PHP_EOL per row after both halves.
Trace $n = 5, $i = 3 on paper — expect digits at both diagonal positions.
Pro Tip: if the center column prints twice, check whether the mirror loop starts at n instead of n-1.
Mistakes that commonly break diagonal mirror number patterns.
Each cell lands on its own line — you get a column, not an X-shaped row.
→ Use echo $j or echo " " inside both inner loops; echo PHP_EOL only after they finish.
Starting at k = n duplicates the center column — row 3 shows two middle digits instead of one spaced pair.
→ Start the mirror loop at $k = $n - 1 and count down to 1.
Omitting echo PHP_EOL after both inner loops glues all rows onto one line.
→ Always call echo PHP_EOL after both diagonal loops complete.
Using $i == $j in the mirror loop prints digits on the wrong diagonal.
→ Main loop uses $i == $j; mirror loop uses $i == $k.
Letters or empty input fail without validation.
→ Call is_numeric($input) before casting to (int).
Using literal 5 in loop bounds instead of variable $n breaks dynamic input.
→ Use one $n variable for both inner loop bounds.
Check these inputs before calling the solution done.
Output is one line: 1 — mirror loop does not run.
Loop never runs — print nothing or show a message.
n < 0Treat as invalid; re-prompt instead of silent empty output.
Large values produce wide rows — fine for labs; use smaller n for quick demos.
Unchecked fgets(STDIN) input leaves $n unset — call is_numeric($input) first.
Use conditional spacing to avoid trailing spaces — see Example 3.
Try these variations to lock in the pattern.
$n = 3, 6, or 8$i==$j and $i==$k positions* instead of numbers on diagonalsk = n..1 instead of n-1..1$n(2*$n-1) (e.g. 45 cells for $n=5).echo stays on the line; PHP_EOL advances — mix them carefully.$n > 0 for interactive programs; $n = 1 prints a single digit.if ($i==$j) echo $j else echo " "; mirror: if ($i==$k) echo $k else echo " ".Quick Takeaway: outer $i=1..$n, inner $j=1..$n with $i==$j, inner $k=$n-1..1 with $i==$k, then echo PHP_EOL.
| Program | Time | Extra space |
|---|---|---|
Fixed $n = 5 (Example 1) | O(n²) | O(1) |
| fgets(STDIN) input (Example 2) | O(n²) | O(1) |
| Star diagonals (Example 3) | O(n²) | O(1) |
The diagonal mirror pattern combines nested loops with diagonal conditions — a natural step after palindrome row patterns. Master the fixed-$n version first, then try fgets(STDIN) input and the star-diagonal variant in Example 3.
Practice the three examples above, then continue to Program 54 for the diamond diagonal number pattern.
Keep echo PHP_EOL after both diagonal halves — one row break per outer iteration.
$i, main diagonal $i == $j, and mirror diagonal $i == $k before codingecho $j or echo " " in left loop; same for mirror loop; then echo PHP_EOL$n ≥ 1 for interactive programsfgets(STDIN) return value before using $n$k = $n (duplicates center)$i == $j in the mirror loop by mistake$n$n = 1 edge casePrint digits only where the main and mirror diagonals cross each row.
Each row prints 2*$n-1 cells
$i = 1..$n
CodeMain: $i==$j; mirror: $i==$k
Logic2*$n-1 cells/row
O(n²)
AnalysisRow $i prints the number on the main diagonal ($i == $j) and on the mirror diagonal ($i == $k). All other cells are spaces — an X-shaped number pattern.
Move on to the diamond diagonal number pattern in the PHP number-pattern series.
12 people found this page helpful