Shape Rule
Odd-width rows
Row i prints digits 1..(2*$i-1) concatenated; leading spaces center each row.

The centered number diamond prints 1, then 123, then 12345, and so on — then mirrors back down to 1. This tutorial covers the top and bottom halves, digit loops, live preview, algorithm steps, worked PHP examples, edge cases, and complexity.
Odd-width rows
Row i prints digits 1..(2*$i-1) concatenated; leading spaces center each row.
Center rows
for ($j = $i; $j < $rows; $j++) prints leading spaces before digits.
1..2*$i-1
echo $k for $k = 1..2*$i-1 — concatenated digits per row.
Bottom half
for ($i = $rows - 1; $i >= 1; $i--) repeats the same row logic downward.
1–20 rows
Pick a row count and draw the centered number diamond instantly in the browser.
Complexity
Prints odd-width rows mirrored across n rows — n² iterations; extra memory stays O(1).
A centered number diamond prints 1, then 123, then 12345, up to the widest row, then mirrors back down. With $rows = 5, the middle line is 123456789.
In PHP you use a top-half loop ($i = 1..$rows), leading spaces, and a digit loop ($k = 1..2*$i-1), then a bottom-half loop mirrors the same logic with $i = $rows-1..1.
It combines centering spaces with a mirrored second loop — the classic two-triangle diamond pattern.
Grow with $i = 1..$rows, then mirror with $i = $rows-1..1.
$rows - $i spaces center each row in the diamond.
Each row prints 1, 123, 12345, … without separators.
Follow Program 43 right-aligned triangle; continue to Program 45 star cross pattern.
In short: top half grows odd-width digit rows with leading spaces; bottom half mirrors the same logic; call echo PHP_EOL after each row.
Given a positive integer $rows (e.g. 5), print a centered number diamond: odd-width digit rows grow to the middle, then mirror downward.
// $rows = 5 (conceptual shape)
// 1
// 123
// 12345
// 1234567
// 123456789
// 1234567
// 12345
// 123
// 1 | Item | Type | Description |
|---|---|---|
$rows | int | Height of the top half (and widest row index). |
| Printed output | text | Centered diamond of concatenated digits; 2*$rows - 1 total lines. |
for $i from 1 to $rows (top half):
print ($rows - $i) spaces
for $k from 1 to 2*$i-1: print $k
print newline
for $i from $rows-1 down to 1 (bottom half):
print ($rows - $i) spaces
for $k from 1 to 2*$i-1: print $k
print newline | Approach | Idea | Best for |
|---|---|---|
| Top + bottom halves | Leading spaces, then digits 1..2*$i-1 | Learning and interviews |
| User-input size | (int) $input; | Flexible console programs |
| Spaced-digit diamond | Print k + " " between digits | More readable output |
| Goal | Pattern |
|---|---|
| Space loop | for ($j = $i; $j < $rows; $j++) (top) or $j = $rows; $j > $i; $j-- (bottom) |
| Top half | for ($i = 1; $i <= $rows; $i++) |
| Bottom half mirror | for ($i = $rows - 1; $i >= 1; $i--) |
| Digit loop | for ($k = 1; $k < $i * 2; $k++) + echo $k |
| End the row | echo PHP_EOL; |
| Program 43 contrast | Right-aligned triangle grows one way; this pattern mirrors rows after the widest line |
Same diamond row — how centering spaces and concatenated digits work together.
$i = 1..$rowsGrowing half — widest row at $i = $rows
$i = $rows-1..1Mirrors top half after the middle row
$k = 1..2*$i-1Prints 1, 123, 12345, … concatenated
trace i=3Dry-run row 3: two spaces then 12345
Reach for this pattern when teaching symmetry — two mirrored loop halves with centering spaces.
Classic follow-up after right-aligned triangles and pyramids.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 43 (right-aligned triangle), then continue to Program 45 (star cross).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a row count between 1 and 20 and draw the centered number diamond in the browser.
Three complete PHP programs — fixed row count, fgets(STDIN) input, and a spaced-digit variant. Click View Output to reveal sample console results.
Print a full diamond with top and bottom loop pairs.
$rows = 5Hard-coded size — top half grows, bottom half mirrors each row.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j < $rows; $j++) {
echo " ";
}
for ($k = 1; $k < $i * 2; $k++) {
echo $k;
}
echo PHP_EOL;
}
for ($i = $rows - 1; $i >= 1; $i--) {
for ($j = $rows; $j > $i; $j--) {
echo " ";
}
for ($k = 1; $k < $i * 2; $k++) {
echo $k;
}
echo PHP_EOL;
} When $i = 1, four leading spaces precede a single 1. When $i = 3, two spaces precede 12345 — odd-width rows build the diamond shape.
Let the user choose the height at runtime.
Read the row count with trim(fgets(STDIN)) (check is_numeric($input) in real apps).
<?php
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . PHP_EOL;
exit(1);
}
$rows = (int) $input;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j < $rows; $j++) {
echo " ";
}
for ($k = 1; $k < $i * 2; $k++) {
echo $k;
}
echo PHP_EOL;
}
for ($i = $rows - 1; $i >= 1; $i--) {
for ($j = $rows; $j > $i; $j--) {
echo " ";
}
for ($k = 1; $k < $i * 2; $k++) {
echo $k;
}
echo PHP_EOL;
} Same nested-loop core as Example 1; only the source of $rows changes. Non-numeric input fails is_numeric($input) — validate before casting to int for safer labs.
Add spaces between digits for a more readable diamond.
Print a space after each digit for a more readable centered diamond.
<?php
$rows = 3;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j < $rows; $j++) {
echo " ";
}
for ($k = 1; $k < $i * 2; $k++) {
echo $k . " ";
}
echo PHP_EOL;
}
for ($i = $rows - 1; $i >= 1; $i--) {
for ($j = $rows; $j > $i; $j--) {
echo " ";
}
for ($k = 1; $k < $i * 2; $k++) {
echo $k . " ";
}
echo PHP_EOL;
} Same two-loop structure; echo $k . " " adds spacing between digits and wider indent pairs keep centering.
echo is built in; use fgets(STDIN) when reading input. Set $rows (fixed or from input).
for ($i = 1; $i <= $rows; $i++) — walks the growing top half of the diamond.
for ($j = $i; $j < $rows; $j++) prints leading spaces before digits.
for ($i = $rows - 1; $i >= 1; $i--) repeats the same row logic downward.
Total digits printed grow on the order of n² — O(n²) time, O(1) extra memory.
$rows = 5, top-half row $i = 3Trace one growing row before the mirror loop runs.
| Step | Loop | Prints |
|---|---|---|
| Spaces | $j = 3, 4 (2 times) | |
| Digits | $k = 1..5 | 12345 |
Row output: 12345 — full diamond has 2*$rows - 1 lines when the bottom half mirrors the top.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: skip the bottom half and get a pyramid instead of a diamond.
Foundation for number diamonds, centered pyramids, and mirrored patterns.
Example: swap digits for stars to build a star diamond variant.
Practice echo vs row newline without complex math.
Example: put echo PHP_EOL inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print 1, 12, 123 without spaces between digits.
Triangular totals make O(n²) concrete for beginners.
Example: count output lines for n = 5 → 9 (2*5-1).
Pair the pattern with fgets(STDIN) and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner 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 loop bounds show up immediately as a lopsided or off-center diamond.
Only loops and console output — no arrays or math libraries.
Invert, center, left-align, or swap digits for stars with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the two-loop diamond first; then try the spaced-digit variant in Example 3.
Small habits that keep number-pattern code clean.
Use $rows (or n) and keep i/j for row/column — or rename to row/col.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Only call echo PHP_EOL after the inner loop finishes the row.
One-liner for spaces: echo " ".repeat($rows - $i)); before the digit loop.
Trace $rows = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of digits per line, you almost certainly put echo PHP_EOL inside the inner loop.
Mistakes that commonly break centered number diamonds.
Each digit lands on its own line — you get a column, not a diamond.
→ Use echo for spaces and numbers; echo PHP_EOL only after both inner loops.
Printing only the top loop gives a pyramid, not a full diamond.
→ Add for ($i = $rows - 1; $i >= 1; $i--) after the top-half loop.
Omitting echo PHP_EOL glues every row onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input leave $rows unset.
→ Use is_numeric($input) and re-prompt on failure.
Using k <= i instead of k < $i * 2 prints too few digits per row.
→ Keep for ($k = 1; $k < $i * 2; $k++) for odd-width rows 1, 3, 5, …
Check these inputs before calling the solution done.
Output is one centered 1 on a single line.
Outer loop never runs — print nothing or show a message.
$rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as odd-width rows mirrored plus spaces — fine for labs, noisy for huge n.
Unchecked fgets(STDIN) leaves $rows unset — call is_numeric($input) first.
Print k + " " between digits — see Example 3.
Try these variations to lock in the pattern.
echo $k . " " like Example 3" " when needed2*$rows-1 digits; the full diamond has 2*$rows-1 lines.echo stays on the line; echo PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 should print one indented 1.echo $k to concatenate digits; add spaces in Example 3 when readability matters.Quick Takeaway: top half grows odd-width rows, bottom half mirrors, leading spaces center each line, then break the row.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Spaced-digit diamond (Example 3) | O(rows²) | O(1) |
The centered number diamond combines nested loops with a simple mirror pattern — a natural step after right-aligned triangles. Master the fixed-$rows version first, then try user input and the spaced-digit diamond.
Practice the three examples above, then continue to Program 45 for the star cross pattern with 0s.
Every row prints 2*$i-1 digits — keep echo PHP_EOL only after both inner loops finish.
echo $k and echo PHP_EOL after each row$rows ≥ 1 for interactive programsfgets(STDIN) return value before using $rowsecho PHP_EOL inside the inner digit loopk <= i instead of k < $i * 2$rows = 1 edge casePrint the pattern the beginner-friendly way.
Row i prints 1..2*$i-1
Definition2*$i-1 digits
CodeMirror i down
Logic2*$rows - 1
I/OO(n²) time
AnalysisEach row prints digits 1..(2*$i-1) concatenated without spaces. Leading spaces center the shape; a second loop mirrors the top half downward.
Move on to the star cross pattern with 0s in the PHP number-pattern series.
12 people found this page helpful