Progressive Digit Reversal Pattern in PHP

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
While Loop + Modulo

What You’ll Learn

How to build a running reversed number in PHP: each line shows $reverse after appending the next last digit of $num.

This pairs % 10 (read last digit) with floor($num / 10) (drop last digit), like Program 60, but accumulates digits into $reverse.

⭐ Pattern Output

Starting from $num = 86523, each line prints the partial reversal after one more digit is moved from the right of $num into $reverse:

Output
3
32
325
3256
32568
1

Complete PHP Program

Shift $reverse left, add $num % 10, print, then truncate $num.

PHP
<?php
$num = 86523;
$reverse = 0;
while ($num != 0) {
    $reverse = $reverse * 10 + ($num % 10);
    echo $reverse . PHP_EOL;
    $num = (int) floor($num / 10);
}

🧠 How It Works

1

Initialize

$num holds the source; $reverse starts at 0.

Setup
2

Append last digit

$reverse * 10 + ($num % 10) shifts old digits left and places the peeled digit in the ones place.

Core step
3

Print partial reversal

echo $reverse . PHP_EOL shows the ladder: 3, then 32, and so on.

Output
4

Strip from source

$num = (int) floor($num / 10) removes the digit you just consumed.

Truncate
=

Full reversal at the end

After the last iteration, $reverse equals the decimal reversal of the original 86523. Work is O(d) for digit count d.

2

Variation — User Input (CLI) Version

Reads a positive integer, then prints the same progressive-reversal ladder (rejects zero or negative input):

PHP
<?php
echo "Enter a positive integer: ";
$num = (int) trim(fgets(STDIN));

if ($num <= 0) {
    echo "Enter a positive integer greater than 0" . PHP_EOL;
    exit;
}

$reverse = 0;
while ($num != 0) {
    $reverse = $reverse * 10 + ($num % 10);
    echo $reverse . PHP_EOL;
    $num = (int) floor($num / 10);
}

💡 Tips for Enhancement

Try These

  • Print only the final $reverse after the loop for a classic full reversal
  • Use intdiv($num, 10) instead of floor for non-negative $num
  • Handle leading zeros in the original number via string logic if needed
  • Push each $reverse into an array for unit tests
  • Compare with strrev((string) $n) for validation on modest inputs

Avoid

  • Forgetting to strip $num (infinite loop)
  • Printing before updating $reverse (wrong order of lines)
  • Assuming % behaves the same for negative numbers without checking PHP rules
  • Letting $reverse overflow PHP integer limits on huge inputs

Key Takeaways

1

reverse * 10 + (num % 10) appends the last digit of num to reverse.

2

floor(num / 10) removes that digit from num.

3

Printing inside the loop reveals each prefix of the full reversal.

4

This is the standard numeric algorithm for reversing an integer digit by digit.

❓ Frequently Asked Questions

Yes. Reading 86523 from right to left yields 32568, which is the final value of $reverse after all digits are processed.
You could build a string and concatenate, but the integer * 10 + digit form matches classic arithmetic reversal.
Move echo after the while loop, or keep the loop and print once when $num becomes 0.
O(d) iterations for d decimal digits of the starting value.

Explore More PHP Number Patterns!

Chain digit tricks with strings, arrays, and palindrome checks for richer exercises.

All Number Patterns →
Did you know?

The same loop body appears in many “reverse integer” interview problems; printing each step turns it into a visible teaching pattern.

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