PHP Number Pattern (Rotating Digits)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

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.

Remember
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.

How to Solve It

One outer loop and two inner loops that rotate the digit sequence.

MethodIdeaBest for
Forward + wrapPrint $i..$rows, then $i-1..1Learning, interviews, exams
User-input rowsSame logic with a variable widthPractice / demos

Pseudocode

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

GoalPattern
Next rotationfor ($i = 1; $i <= $rows; $i++)
Forward segmentfor ($j = $i; $j <= $rows; $j++) echo $j;
Wrap segmentfor ($k = $i; $k > 1; $k--) echo $k - 1;
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $j / echo $k - 1Stays on the same lineEach digit (no spaces)
echo PHP_EOLEnds the current lineAfter both inner loops

Print digits without a newline, then end the row once.

Live Preview

Change the row count and the rotating pattern updates instantly — each row stays the same width.

Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 5 digits/row
12345
23451
34521
45321
54321

Worked Walkthrough — $rows = 4

Trace the forward climb and the wrap-around. Every row has exactly 4 digits.

$iForward / wrapPrinted row
11234 / (none)1234
2234 / 12341
334 / 213421
44 / 3214321

Forward length is $rows - $i + 1; wrap length is $i - 1. Sum = $rows.

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
<?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;
}

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
<?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
<?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 $k

Print $k instead of $k - 1

The wrap would reprint $i and skip 1. Always print $k - 1.

wrong wrap end

Run wrap while $k >= 1

That prints an extra 0. Keep $k > 1.

PHP_EOL early

echo PHP_EOL between the two halves

That splits forward and wrap onto separate lines. End the row only after both loops.

rows = 1

Single 1

Only the forward loop runs; the wrap loop never executes.

rows ≤ 0

Empty 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.

Time and Space Complexity

ProgramTimeExtra 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².

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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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