A diamond diagonal number pattern is Program 53’s X-shape for rows 1..$n, then the same rows again from $n − 1 down to 1 — a full diamond of diagonals.
Remember
Rule: print diagonal-mirror rows for i = 1..n
then again for i = n-1..1
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1 ← n = 5 (9 rows)
Unlike Program 53 (top half only), here a second outer loop mirrors the pattern vertically without duplicating the center row. Program 55 continues with a diagonal-fill number triangle.
Approach
How to Solve It
Reuse the same diagonal row logic twice: once ascending, once descending from $n − 1. End each row with echo PHP_EOL.
Method
Idea
Best for
Fixed n
Two outer loops; $i==$j / $i==$k
Labs and demos
CLI input
Same logic; read $n at runtime
Interactive practice
Star diagonals
Print * instead of the digit
Visual diamond practice
Pseudocode
Pseudocode
function printRow(i, n):
for j from 1 to n:
if i == j: print j else print space
for k from n-1 down to 1:
if i == k: print k else print space
print newline
for i from 1 to n: printRow(i, n)
for i from n-1 down to 1: printRow(i, n)
Cheat sheet
Goal
Pattern
Set size
$n = 5;
Top half
for ($i = 1; $i <= $n; $i++)
Bottom half
for ($i = $n - 1; $i >= 1; $i--)
Main / mirror
Same as Program 53: $i==$j, $i==$k
End the row
echo PHP_EOL;
Total rows
2 * $n - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j / echo " "
Stays on the same line
Each digit or space in both halves
echo PHP_EOL
Ends the current line
After both halves of a row finish
Build each row with echo, then break once.
Try it
Live Preview
Change n and the full diamond diagonal pattern updates instantly.
Whole numbers from 3 to 7. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · rows = 9
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1
Trace
Worked Walkthrough
Trace $n = 3 — five rows: top 1..3, then bottom 2..1.
Half
$i
Printed row
Top
1
1 1
Top
2
2 2
Top (center)
3
3
Bottom
2
2 2
Bottom
1
1 1
The bottom loop starts at $n − 1 so the center row ($i = $n) appears only once.
Code
PHP Programs
Three complete programs: fixed $n = 5, CLI input, and star diagonals. Use View Output for sample results.
Example 1 — Fixed $n = 5
Hard-coded size — top half 1..$n, then bottom half $n − 1..1.
echo 10 prints two characters and breaks column alignment. Labs usually stick to 1–9.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input / star (Examples 1–3)
O(n²)
O(1)
There are 2n − 1 rows, and each row checks about 2n − 1 cells, so total work is still O(n²).
Remember
Key Takeaways
Rule: Program 53 rows for 1..$n, then again for $n − 1..1.
Skip duplicate: bottom half starts at $n − 1 so the center appears once.
echo vs PHP_EOL: digits and spaces stay on the line; echo PHP_EOL advances after each row.
Next step: Program 55 fills a triangle using diagonal jump logic.
One line: print the X for 1..$n, then mirror it for $n − 1..1.
Frequently Asked Questions
Program 53 prints only $n rows (the top half). Program 54 adds a second outer loop from $n-1 down to 1 to mirror the output vertically.
Starting at $n would duplicate the middle row (the widest point). $n-1 down to 1 mirrors without repeating the center.
For size $n, the diamond has 2*$n-1 rows: $n for the top half and $n-1 for the bottom half.
At $i=$n, both diagonals meet at the same position, so only one digit appears on the center row.
Yes. Use a variable $n — both outer loops reuse the same diagonal row logic.
Replace the printed digit with * when $i==$j or $i==$k in both outer loops — see Example 3.
echo stays on the same line for each digit or space. echo PHP_EOL ends the row after both halves of a row finish.
O(n²) for (2*$n-1) rows. Each row checks O(n) cells in two inner loops.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.
🤔
Did you know?
Program 54 prints the diagonal mirror pattern (Program 53) for rows 1..$n, then repeats the same logic for rows $n-1..1 — forming a full diamond with numbers on both diagonals.