0
0
PhpProgramBeginner · 2 min read

PHP Program to Reverse a Number with Output and Explanation

You can reverse a number in PHP by repeatedly getting the last digit using % 10, building the reversed number by multiplying the current reversed number by 10 and adding the digit, and removing the last digit from the original number using / 10. For example: $reversed = 0; while ($num > 0) { $reversed = $reversed * 10 + $num % 10; $num = (int)($num / 10); }
📋

Examples

Input1234
Output4321
Input1000
Output1
Input0
Output0
🧠

How to Think About It

To reverse a number, think of peeling off the last digit one by one and adding it to a new number from left to right. Use the remainder operator % to get the last digit, then add it to the reversed number after shifting the reversed number left by one digit (multiply by 10). Remove the last digit from the original number by dividing by 10 and repeating until the original number is zero.
📐

Algorithm

1
Get the input number.
2
Initialize reversed number as 0.
3
While the input number is greater than 0:
4
- Extract the last digit using modulus 10.
5
- Multiply reversed number by 10 and add the last digit.
6
- Remove the last digit from input number by dividing by 10 (integer division).
7
Return the reversed number.
💻

Code

php
<?php
$num = 1234;
$reversed = 0;
while ($num > 0) {
    $digit = $num % 10;
    $reversed = $reversed * 10 + $digit;
    $num = (int)($num / 10);
}
echo $reversed;
?>
Output
4321
🔍

Dry Run

Let's trace reversing 1234 through the code

1

Initial values

$num = 1234, $reversed = 0

2

First loop iteration

digit = 1234 % 10 = 4; reversed = 0 * 10 + 4 = 4; num = (int)(1234 / 10) = 123

3

Second loop iteration

digit = 123 % 10 = 3; reversed = 4 * 10 + 3 = 43; num = (int)(123 / 10) = 12

4

Third loop iteration

digit = 12 % 10 = 2; reversed = 43 * 10 + 2 = 432; num = (int)(12 / 10) = 1

5

Fourth loop iteration

digit = 1 % 10 = 1; reversed = 432 * 10 + 1 = 4321; num = (int)(1 / 10) = 0

6

Loop ends

num is 0, exit loop, reversed = 4321

numdigitreversed
123444
123343
122432
114321
💡

Why This Works

Step 1: Extract last digit

Using % 10 gets the last digit of the number, which we add to the reversed number.

Step 2: Build reversed number

Multiply the current reversed number by 10 to shift digits left, then add the extracted digit.

Step 3: Remove last digit

Divide the original number by 10 and convert to integer to remove the last digit for the next iteration.

🔄

Alternative Approaches

String reversal
php
<?php
$num = 1234;
$reversed = strrev((string)$num);
echo (int)$reversed;
?>
This method converts the number to a string, reverses it, then converts back to integer. It's simpler but uses string functions.
Recursive reversal
php
<?php
function reverseNumber($num, $reversed = 0) {
    if ($num == 0) return $reversed;
    return reverseNumber((int)($num / 10), $reversed * 10 + $num % 10);
}
echo reverseNumber(1234);
?>
This uses recursion to reverse the number. It is elegant but uses more memory due to function calls.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once for each digit in the number, so time grows linearly with the number of digits.

Space Complexity

Only a few variables are used, so space is constant regardless of input size.

Which Approach is Fastest?

The arithmetic loop method is fastest and uses least memory; string reversal is simpler but slightly slower due to string operations.

ApproachTimeSpaceBest For
Arithmetic loopO(d)O(1)Performance and memory efficiency
String reversalO(d)O(d)Simplicity and quick coding
Recursive reversalO(d)O(d)Elegant code but uses more memory
💡
Use integer division (int)($num / 10) to remove the last digit safely in PHP.
⚠️
Forgetting to cast the division result to integer causes infinite loops or wrong results.