5+ PHP Programs to Reverse a Number

This post contains a total of 5+ Hand-Picked PHP Programs to Reverse a Number with Source Codes. All the programs are made using PHP Programming Language.

You can use the source code of these programs for educational purpose with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Sourabh Dubey

Made by Sourabh Dubey. A simple PHP Program to reverse a number / integer. Enter your number in $num. ( Source )

print reverse number=>4321
<?php
    echo"print reverse number=>";
    $num = 1234;
    while($num>=1)
    {
        $rev = $num%10;
        $num = $num/10;
        echo $rev;
    }
?>

2. By Daniel sakti

Made by Daniel sakti. Program to reverse a integer. Enter the integer in $a. ( Source )

input =12345
Output =54321
<?php
    $a = "12345";
    $n= strlen($a);
        echo "input =".$a;
        echo "<hr/>";
    for($i=0;$i<=floor(($n-1)/2);$i++)
      {
        $output = $a[$i];
        $a[$i] = $a[$n-1-$i];
        $a[$n-1-$i] = $output;
      }
      echo "Output =".$a ;
?>

3. By Malek Alsset

Made by Malek Alsset. Iterative function to reverse digits of num. ( Source )

Reverse of number is 2654
<?php 
function reversDigits($num) 
{ 
    $rev_num = 0; 
    while($num > 1) 
    { 

        $rev_num = $rev_num * 10 +  
        $num % 10; 
        $num = (int)$num / 10; 

    } 
    return $rev_num; 
} 
$num = 4562; 
echo "Reverse of number is ",  
       reversDigits($num); 
?>

4. By Shivam Dubey

Made by Shivam Dubey. Enter the number you want to reverse in $a. ( Source )

the reverse number is :1351
<?php

$a=1531;
$b=$a;
$sum=0;
while ($a>0)
{
    $r=$a%10;
    $sum=$sum*10+$r;
    $a=(int)($a/10);
} 

    echo "the reverse number is :$sum";
 
?>

5. By angad singh

Made by angad singh. Enter your number in revFnc(51). ( Source )

15
<?php
function revFnc($num)
{
    $re=$num%10;
    $rev2=floor($num/10);
    echo $re.$rev2;
}
revFnc(51);
?>

6. By rohit pandit

Made by rohit pandit. ( Source )

<?php 
$o=889;
$r;
$rev=0;
while($o!=0){
    $r=$o%10;
    $rev=$rev*10+$r;
    $o=$o/10;
}
echo $rev;
?>