PHP Number Pattern (Rotating Digits)

Definition
What Is This Pattern?
A rotating number pattern prints a fixed-width row that starts at $i, climbs to $rows, then wraps back down through $i-1..1.
Rule: print i..rows, then (i-1)..1 (always rows digits)
12345
23451
34521
45321
54321 ← 5 rows
In PHP use two inner loops: forward $j from $i to $rows, then wrap $k from $i down to 2 printing $k - 1.
Approach
How to Solve It
One outer loop and two inner loops that rotate the digit sequence.
| Method | Idea | Best for |
|---|
| Forward + wrap | Print $i..$rows, then $i-1..1 | Learning, interviews, exams |
| User-input rows | Same logic with a variable width | Practice / demos |
Pseudocode
for i from 1 to rows:
for j from i to rows:
print j
for k from i down to 2:
print (k - 1)
print newline
Cheat sheet
| Goal | Pattern |
|---|
| Next rotation | for ($i = 1; $i <= $rows; $i++) |
| Forward segment | for ($j = $i; $j <= $rows; $j++) echo $j; |
| Wrap segment | for ($k = $i; $k > 1; $k--) echo $k - 1; |
| End the row | echo PHP_EOL; |
Printing Numbers vs Starting a New Line
| API | Effect | Use for |
|---|
echo $j / echo $k - 1 | Stays on the same line | Each digit (no spaces) |
echo PHP_EOL | Ends the current line | After both inner loops |
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the rotating pattern updates instantly — each row stays the same width.
Trace
Worked Walkthrough — $rows = 4
Trace the forward climb and the wrap-around. Every row has exactly 4 digits.
$i | Forward / wrap | Printed row |
|---|
1 | 1234 / (none) | 1234 |
2 | 234 / 1 | 2341 |
3 | 34 / 21 | 3421 |
4 | 4 / 321 | 4321 |
Forward length is $rows - $i + 1; wrap length is $i - 1. Sum = $rows.
Code
PHP Programs
Three complete programs: fixed height, CLI input, and a compact 3-row demo. Use View Output for sample results.
Example 1 — Fixed $rows = 5
Forward loop prints $i..$rows; wrap loop prints $i-1..1 via $k - 1.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= $rows; $j++)
echo $j;
for ($k = $i; $k > 1; $k--)
echo $k - 1;
echo PHP_EOL;
}
12345
23451
34521
45321
54321
How It Works
1. Outer loop. $i is the start digit of the rotation for that row.
2. Forward. Print from $i up to $rows — e.g. row 3 prints 345.
3. Wrap. Print $k - 1 while $k runs from $i down to 2 — completing 34521.
Example 2 — User Input
Read the row count from STDIN; each row still prints exactly $rows digits.
<?php
echo "Enter rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
echo "Please enter a positive whole number." . PHP_EOL;
exit(1);
}
$rows = (int) $input;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= $rows; $j++)
echo $j;
for ($k = $i; $k > 1; $k--)
echo $k - 1;
echo PHP_EOL;
}
How It Works
1. Validate rows. Check is_numeric; require $rows >= 1.
2. Same rotation. Three rows of width 3 end at 321.
Example 3 — Compact $rows = 3
A smaller fixed demo — same forward-and-wrap idea, easier to trace by hand.
<?php
$rows = 3;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= $rows; $j++)
echo $j;
for ($k = $i; $k > 1; $k--)
echo $k - 1;
echo PHP_EOL;
}
How It Works
1. Same rules. Forward prints $i..$rows; wrap prints $i-1..1.
2. Quick check. The last row is a full reverse: 321.
Edge Cases & Pitfalls
Check these before calling the solution done.
print $kPrint $k instead of $k - 1
The wrap would reprint $i and skip 1. Always print $k - 1.
wrong wrap endRun wrap while $k >= 1
That prints an extra 0. Keep $k > 1.
PHP_EOL earlyecho PHP_EOL between the two halves
That splits forward and wrap onto separate lines. End the row only after both loops.
rows = 1Single 1
Only the forward loop runs; the wrap loop never executes.
rows ≤ 0Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
Bad input(int) alone is not enough
Prefer is_numeric after trim(fgets(STDIN)) so non-numeric input does not silently become 0.
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| Fixed / compact (Examples 1, 3) | O(n²) | O(1) |
| User input (Example 2) | O(n²) | O(1) |
There are n rows and each prints exactly n digits, so total work is n².
Remember
Key Takeaways
Rule: print $i..$rows, then $i-1..1 — each row has exactly $rows digits.
Wrap: use echo $k - 1 while $k runs from $i down to 2.
echo vs PHP_EOL: digits stay on the line; echo PHP_EOL advances after both halves.
Next step: Program 40 prints an alternating 1 and 0 pattern.
One line: for each row $i, print a forward climb to $rows and a wrap back to 1 so the sequence rotates.
Frequently Asked Questions
For 5 rows: 12345, 23451, 34521, 45321, 54321 — each row starts at the row number and wraps back to 1.
The first loop prints $i..$rows (forward segment). The second loop prints $i-1 down to 1 (wrap segment). Together they always produce $rows digits.
After printing 2 3 4 5, the wrap loop prints $k-1 when $k runs from $i down to 2 — for $i=2 that prints 1.
Exactly $rows digits every time — ($rows - $i + 1) forward plus ($i - 1) wrap = $rows.
Program 37 builds palindrome rows ($i..2 then 1..$i). Program 39 rotates: $i..$rows then $i-1..1.
Replace 5 with $rows in the outer loop bound — see Example 2.
O(n²) for n rows because each row prints n digits.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
Only one row prints — a single digit 1.
🤔
Did you know?
Each row starts at $i, prints $i..$rows, then wraps with $i-1..1. Row $i always prints exactly $rows digits — total digits = n².
Next: Alternating 1 and 0 Pattern
Print rows of ones and zeros that shrink and alternate each line.
Program 40 tutorial →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
6 people found this page helpful