Progressive Digit Reversal Pattern in PHP

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:
3
32
325
3256
32568Complete PHP Program
Shift $reverse left, add $num % 10, print, then truncate $num.
<?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
Initialize
$num holds the source; $reverse starts at 0.
Append last digit
$reverse * 10 + ($num % 10) shifts old digits left and places the peeled digit in the ones place.
Print partial reversal
echo $reverse . PHP_EOL shows the ladder: 3, then 32, and so on.
Strip from source
$num = (int) floor($num / 10) removes the digit you just consumed.
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.
Variation — User Input (CLI) Version
Reads a positive integer, then prints the same progressive-reversal ladder (rejects zero or negative input):
<?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
$reverseafter the loop for a classic full reversal - Use
intdiv($num, 10)instead offloorfor non-negative$num - Handle leading zeros in the original number via string logic if needed
- Push each
$reverseinto 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
$reverseoverflow PHP integer limits on huge inputs
Key Takeaways
reverse * 10 + (num % 10) appends the last digit of num to reverse.
floor(num / 10) removes that digit from num.
Printing inside the loop reveals each prefix of the full reversal.
This is the standard numeric algorithm for reversing an integer digit by digit.
❓ Frequently Asked Questions
86523 from right to left yields 32568, which is the final value of $reverse after all digits are processed.* 10 + digit form matches classic arithmetic reversal.echo after the while loop, or keep the loop and print once when $num becomes 0.d decimal digits of the starting value.Explore More PHP Number Patterns!
Chain digit tricks with strings, arrays, and palindrome checks for richer exercises.
The same loop body appears in many “reverse integer” interview problems; printing each step turns it into a visible teaching pattern.
12 people found this page helpful
