Shape Rule
$m++ / $m--
Row $i: increase $i digits, then decrease $i-1 digits.

The palindrome row pattern prints 1, 232, 34543, 4567654, 567898765 — each row increases then mirrors downward. This tutorial covers dual inner-loop logic, the $m = $m - 2 center step, live preview, worked PHP examples, edge cases, and O(n²) complexity.
$m++ / $m--
Row $i: increase $i digits, then decrease $i-1 digits.
$m = $i
Each row begins at its row number — row 4 starts from 4.
$m = $m - 2
Between loops, step back so the peak digit is not printed twice.
2i - 1 digits
Row $i has 2*$i-1 digits — width grows each line.
3–12 rows
Pick a row count and draw the palindrome row pattern instantly in the browser.
Complexity
Total digits ≈ n² — work grows quadratically.
An increasing-decreasing palindrome row pattern starts each row at $i, prints an increasing run, then mirrors downward. Row 3 gives 34543; row 4 gives 4567654.
In PHP: outer for ($i = 1; $i <= $rows; $i++), $m = $i, inner increase loop echo $m++, $m = $m - 2, inner decrease loop echo $m--, then echo PHP_EOL after both inner loops.
It is a dual inner-loop exercise that builds palindrome-like rows with increase then decrease segments.
$i = 1..$rows picks each row number.
First loop echo $m++; after $m = $m - 2, second loop echo $m--.
Each row resets $m = $i — increase loop then decrease loop per row.
Follow Program 51 alternating triangle; continue to Program 53 diagonal mirror.
In short: outer $i=1..$rows, $m=$i, inner $j=1..$i echo $m++, $m=$m-2, inner $k=1..$i-1 echo $m--, then echo PHP_EOL.
Given $rows = 5, print five lines: 1, 232, 34543, 4567654, 567898765.
// $rows = 5 (conceptual output)
// 1
// 232
// 34543
// 4567654
// 567898765 | Item | Type | Description |
|---|---|---|
$rows | int | How many lines to print (typically ≥ 1). |
$i, $j, $k, $m | int | Row index $i; variable $m with increase loop and decrease loop. |
| Printed output | text | 2*$i-1 digits on row $i — palindrome-like width. |
for i from 1 to rows:
$m = $i
for j from 1 to i: print m; m = m + 1
$m = $m - 2
for k from 1 to i-1: print m; m = m - 1
newline | Approach | Idea | Best for |
|---|---|---|
| Dual inner loops | echo $m++ then echo $m-- with $m = $m - 2 between | Palindrome width — 2*$i-1 digits per row |
| fgets(STDIN) input | (int) $input after is_numeric($input) | User-chosen row count |
| Spaced digits | Print space after each digit in both loops | Easier reading for wider rows — Example 3 |
| Goal | Pattern |
|---|---|
| Set rows | $rows = 5; |
| Outer loop | for ($i = 1; $i <= $rows; $i++) |
| Row start | $m = $i; |
| Increase loop | for ($j = 1; $j <= $i; $j++) echo $m++; |
| Center step | $m = $m - 2; |
| Decrease loop | for ($k = 1; $k < $i; $k++) echo $m--; |
| Row break | echo PHP_EOL; after both inner loops |
| Program 51 contrast | Alternating triangle uses running counter $k; this pattern uses dual inner loops with $m = $m - 2 |
How outer row selection, increase loop, center step, and decrease loop work together.
for ($i = 1; $i <= $rows; $i++)Picks row number $i — also the starting digit.
$m = $i
for ($j = 1; $j <= $i; $j++)
echo $m++Prints $i ascending digits on row $i.
$m = $m - 2
for ($k = 1; $k < $i; $k++)
echo $m--Prints i-1 descending digits — mirrors without duplicating peak.
trace $i=3Dry-run row 3: increasing 345 + decreasing 43 → 34543.
Reach for this pattern when teaching dual inner loops, palindrome rows, and the $m = $m - 2 center step.
Classic follow-up after alternating triangle patterns like Program 51.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 51 (alternating triangle), then continue to Program 53 (diagonal mirror).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in dual inner loops, $m = $m - 2, and O(n²) thinking.
Choose a row count and draw the palindrome row pattern in the browser.
Three complete PHP programs — fixed $rows = 5, fgets(STDIN) input, and a spaced-digit variant. Click View Output to reveal sample console results.
Print five rows with increase then decrease loops — palindrome-like rows.
$rows = 5Hard-coded size — start $m = $i, increase loop, $m = $m - 2, then decrease loop.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
$m = $i;
for ($j = 1; $j <= $i; $j++) {
echo $m++;
}
$m = $m - 2;
for ($k = 1; $k < $i; $k++) {
echo $m--;
}
echo PHP_EOL;
} When $i = 3, increasing prints 345, then $m = $m - 2 gives decreasing 43 — output 34543. Row 1 has only the increasing half — output is 1.
Read $rows with fgets(STDIN) for flexible output size.
Same dual inner loops; row count comes from user input.
<?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++) {
$m = $i;
for ($j = 1; $j <= $i; $j++) echo $m++;
$m = $m - 2;
for ($k = 1; $k < $i; $k++) echo $m--;
echo PHP_EOL;
} Identical loop structure to Example 1; only the row count is dynamic.
Add spaces between digits for easier reading.
Print a space after each digit in both inner loops.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
$m = $i;
for ($j = 1; $j <= $i; $j++) {
echo $m++ . " ";
}
$m = $m - 2;
for ($k = 1; $k < $i; $k++) {
echo $m-- . " ";
}
echo PHP_EOL;
} Same dual-loop logic; only the output format adds spaces between digits.
echo is built in; use fgets(STDIN) when reading input. Set $rows (e.g. 5).
for ($i = 1; $i <= $rows; $i++) — one iteration per output line.
First loop echo $m++ for $i steps; after $m = $m - 2, second loop echo $m-- for $i-1 steps.
echo PHP_EOL; after both inner loops moves to the next row.
Total prints ≈ n² digits — O(n²) time, O(1) extra memory.
$i = 3Trace row 3 to see how increase, $m = $m - 2, and decrease build 34543.
| Phase | $m | Row so far |
|---|---|---|
| Start | 3 | — |
| Increase $j=1,2,3 | 6 | 345 |
| $m = $m - 2 | 4 | 345 |
| Decrease $k=1 | 3 | 3454 |
| Decrease $k=2 | 2 | 34543 |
After both inner loops, 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 row count — see Example 2.
Each row mirrors after the peak — $m = $m - 2 prevents duplicating the center digit.
Example: compare row 3 (34543) — increasing 345 plus decreasing 43.
Practice echo vs PHP_EOL for multi-line vs single-line output.
Example: use echo $m++ . " " in both loops for spaced output — Example 3.
Add spaces after each digit in both inner loops for readability.
Example: print $rows=5 with spaced output and compare readability.
Palindrome width 2i-1 makes O(n²) concrete for beginners.
Example: count digits for rows=5 → 1+3+5+7+9 = 25 prints.
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 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.
Missing $m = $m - 2 shows immediately — center digit appears twice in each row.
Only loops and console output — no arrays or math libraries.
Change $rows, use fgets(STDIN), or add spaces between digits in both inner loops.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the dual inner loops first; then try fgets(STDIN) input and the spaced-digit variant in Example 3.
Small habits that keep number-pattern code clean.
Use $rows for height and $i/$j for row/column indices.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Run increase loop, apply $m = $m - 2, run decrease loop, then echo PHP_EOL.
Use echo $m++ and echo $m-- in the inner loops; one echo PHP_EOL per row after both loops.
Trace $rows = 5, $i = 3 on paper — expect 34543.
Pro Tip: if rows have duplicated center digits, check whether you forgot $m = $m - 2.
Mistakes that commonly break increasing-decreasing palindrome row patterns.
Each digit lands on its own line — you get a column, not a palindrome row.
→ Use echo $m++ or echo $m-- inside the inner loops; echo PHP_EOL only after they finish.
Without $m = $m - 2, the peak digit prints twice — row 3 becomes 345543 instead of 34543.
→ Always apply $m = $m - 2 between the increase and decrease loops.
Omitting echo PHP_EOL after the inner loop glues all rows onto one line.
→ Always call echo PHP_EOL after the inner loop completes.
Using k <= i in the decrease loop duplicates the peak digit.
→ Use $k < $i for the decrease loop — exactly i-1 descending digits.
Letters or empty input fail without validation.
→ Call is_numeric($input) before casting to (int).
Using literal 10 in loop bounds instead of variable $rows breaks dynamic input.
→ Use one $rows variable for the outer loop bound.
Check these inputs before calling the solution done.
Output is one line: 1.
Loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Large row counts produce many values — fine for labs; use smaller n for quick demos.
Unchecked fgets(STDIN) input leaves $rows unset — call is_numeric($input) first.
Use conditional spacing to avoid trailing spaces — see Example 3.
Try these variations to lock in the pattern.
$rows = 3, 6, or 82i-1 digits and mirrors after the peak$m = $i * i instead of $m = $in² (e.g. 25 digits for rows=5).echo stays on the line; PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 prints one value.echo $m++; after $m = $m - 2, decrease loop uses echo $m-- with $k < $i.Quick Takeaway: outer $i=1..$rows, $m=$i, inner $j=1..$i echo $m++, $m=$m-2, inner $k=1..$i-1 echo $m--, then echo PHP_EOL.
| Program | Time | Extra space |
|---|---|---|
Fixed $rows = 5 (Example 1) | O(n²) | O(1) |
| fgets(STDIN) input (Example 2) | O(n²) | O(1) |
| Spaced digits (Example 3) | O(n²) | O(1) |
The palindrome row pattern combines dual inner loops with $m = $m - 2 — a natural step after alternating triangle patterns. Master the fixed-$rows version first, then try fgets(STDIN) input and the spaced-digit variant in Example 3.
Practice the three examples above, then continue to Program 53 for the diagonal mirror number pattern.
Keep echo PHP_EOL after both inner loops — one row break per outer iteration.
$i, $m = $i, increase loop, $m = $m - 2, and decrease loop before codingecho $m++ and echo $m--, then echo PHP_EOL after both inner loops$rows ≥ 1 for interactive programsfgets(STDIN) return value before using $rowsk <= i in decrease loop (duplicates peak)$m = $m - 2 between the two inner loops$rows$rows = 1 edge casePrint the pattern the beginner-friendly way.
Each row prints 2*$i-1 digits
$i = 1..$rows
CodeUp: echo $m++; down: echo $m--
Logic2*$i-1 digits/row
O(n²)
AnalysisEach row starts from $i, prints an increasing run of length $i, then a decreasing run of length $i-1. The key step is $m = $m - 2 so the peak digit is not duplicated — row 3 gives 34543.
Move on to the diagonal mirror number pattern in the PHP number-pattern series.
12 people found this page helpful