To reverse a number in PHP without the use of integrated function. First we want to claim a variable to save the reversed number, and first of all assign 0 value to it. Now, extract the final digit from the unique number, and multiply the opposite number with 10, then add the extracted digit to reversed number, and divide the unique number by 10.
A number can be written in reverse order.
For example
12345 = 54321
/* PHP program to reverse a number without using function */
$num = 12345;
$revnum = 0;
while ($num != 0)
{
$a = $num % 10;
$revnum = ($revnum * 10) + $a
//below cast is essential to round remainder towards zero
$num = (int)($num / 10);
}
echo "Reverse number: $revnum\n";
In the above example we reverse the number without using PHP function. This is very important technical php interview questions you should know this trick to reverse the number.
Now we look using PHP function reverse the number
$num = 1234567;
$rev = strrev($num);
echo "original number is = <b> $num </b> <br/>";
echo "reverse number is = <b> $rev </b> <br/>";
Related Articles
Password Strength Checker Using JavaScript
How To Find Reverse A Number In Python