PHP Number Pattern (Progressive Reverse)

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

What Is This Pattern?

A progressive reverse-build peels digits from the right of a number and appends each one to a growing reverse — from 86523 you get 3, 32, 325, 3256, 32568.

Remember
Rule: reverse = 0
      while num != 0:
        reverse = reverse * 10 + (num % 10)
        print reverse
        num = intdiv(num, 10)

3
32
325
3256
32568     ← start = 86523

Unlike Program 60 (prints the shrinking original), here you print the reverse after every digit is appended. Program 62 continues with a perfect-square spiral grid.

How to Solve It

Take $num % 10, append it with $reverse * 10 + digit, print $reverse, then intdiv($num, 10). Repeat while $num != 0.

MethodIdeaBest for
Fixed numberwhile + modulo append + intdivLabs and demos
CLI inputSame loop; read $num at runtimeInteractive practice
String accumulatorAppend digits as a stringAvoiding integer overflow

Pseudocode

Pseudocode
num = starting value
reverse = 0
while num != 0:
    reverse = reverse * 10 + (num % 10)
    print reverse and newline
    num = integer divide num by 10

Cheat sheet

GoalPattern
Set start$num = 86523; $reverse = 0;
Last digit$num % 10
Append$reverse = $reverse * 10 + ($num % 10);
Print lineecho $reverse . PHP_EOL;
Drop processed digit$num = intdiv($num, 10);

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $reverseStays on the same lineRare here — usually you want a full line
echo $reverse . PHP_EOLPrints value and ends the lineEach progressive reverse (this pattern)

Same idea as C# WriteLine: one snapshot of $reverse per line.

Live Preview

Change the starting number and the progressive reverse updates instantly.

Positive whole numbers up to 9 digits. Tap a chip or type a value — the preview redraws as you go.

Live result start = 86523 · lines = 5
3
32
325
3256
32568

Worked Walkthrough

Trace $num = 86523 — each step appends one digit to $reverse, then shrinks $num.

IterationDigit (% 10)$reverseAfter intdiv
1338652
2232865
3532586
4632568
58325680 → done

Print happens after each append — that is what makes the reverse progressive instead of only final.

PHP Programs

Three complete programs: fixed $num = 86523, CLI input, and a string accumulator. Use View Output for sample results.

Example 1 — Fixed $num = 86523

Build $reverse digit by digit and print after each append.

PHP
<?php
$num = 86523;
$reverse = 0;

while ($num != 0) {
    $reverse = $reverse * 10 + ($num % 10);
    echo $reverse . PHP_EOL;
    $num = intdiv($num, 10);
}
?>

How It Works

1. Peel a digit. $num % 10 reads the rightmost digit (first: 3).

2. Append and print. $reverse * 10 + digit grows the reverse; echo shows the snapshot.

3. Shrink. intdiv($num, 10) drops the processed digit until $num is 0.

Example 2 — User Input

Read the starting number from the CLI. Prefer is_numeric (shown below).

PHP
<?php
echo "Enter a number: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input == 0) {
    echo "Enter a non-zero whole number." . PHP_EOL;
    exit(1);
}
$num = abs((int) $input);
$reverse = 0;

while ($num != 0) {
    $reverse = $reverse * 10 + ($num % 10);
    echo $reverse . PHP_EOL;
    $num = intdiv($num, 10);
}
?>

How It Works

1. Validate input. is_numeric rejects letters; reject zero so the loop would otherwise print nothing.

2. Use abs. abs((int) $input) keeps the digit walk positive.

3. Same while loop. Append, print, and intdiv match Example 1 — only the source of $num changes.

Example 3 — String Accumulator

Same visual result; append digits as a string so large inputs never overflow an int.

PHP
<?php
$num = 86523;
$reverse = "";

while ($num != 0) {
    $reverse .= (string) ($num % 10);
    echo $reverse . PHP_EOL;
    $num = intdiv($num, 10);
}
?>

How It Works

1. Same digits. Still peel with $num % 10 and shrink with intdiv.

2. Concatenate. $reverse .= (string) ($num % 10) grows without integer limits.

3. Same look. For classroom sizes like 86523, output matches Examples 1–2.

Edge Cases & Pitfalls

Check these before calling the solution done.

print last only

Printing only after the loop

That shows one final reverse. Echo $reverse inside the loop for progressive lines.

wrong order

Dividing before appending

That skips the first last digit. Append with % 10, print, then intdiv.

num = 0

Starting at zero

The loop never runs — no output. Validate and ask for a non-zero value.

trailing 0

Numbers ending in zero

120 builds 0, then 2, then 21 — leading zeros disappear in integer form (first line may print 0).

overflow

Very large integers

On 32-bit PHP, huge reverses can overflow. Use the string accumulator (Example 3) for big inputs.

single digit

One-digit start

Output is a single line equal to the number itself (e.g. 7).

Time and Space Complexity

ProgramTimeExtra space
while + int reverse (Examples 1–2)O(d)O(1)
String accumulator (Example 3)O(d)O(d) for the string

For d digits, the loop runs exactly d times — linear in the digit count.

Key Takeaways

  • Rule: append $num % 10 to $reverse, print, then intdiv.
  • Vs Program 60: that shrinks the original; this grows the reverse.
  • echo + PHP_EOL: one call per line prints the snapshot and advances.
  • Next step: Program 62 fills a 10×10 grid in a perfect-square spiral.

One line: peel last digits into a growing reverse and print every snapshot.

Frequently Asked Questions

Because 3 is the last digit of 86523. The program takes $num % 10 first and appends it to $reverse.
Each step appends the next last digit to $reverse: 3, then 2 → 32, then 5 → 325, then 6 → 3256.
Once all digits of 86523 are processed, $reverse becomes 32568 and $num becomes 0, ending the loop.
Related — you build the reverse but print it after every step rather than only the final reverse.
Program 60 prints the shrinking original (86523, 8652, …). Program 61 prints the growing reverse (3, 32, 325, …).
Trailing zeros become leading zeros in the reverse, but leading zeros are not shown when printing integers.
Here each line uses echo $reverse . PHP_EOL — one call prints the value and ends the line.
O(d) where d is the number of digits — one loop iteration per digit.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.

Did you know?

Each step appends the last digit of $num to $reverse with $reverse = $reverse * 10 + ($num % 10), then shrinks $num with intdiv($num, 10). From 86523: 3, 32, 325, 3256, 32568 — O(d) time.

Next: Perfect Square Spiral

Continue with a 10×10 grid filled in spiral order.

Program 62 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.

12 people found this page helpful