Press "Enter" to skip to content

How to reverse value of integer or string in php?

0

The logic of the below methods will help you to reverse the integer in any language, here I am doing it in PHP.

So let’s create a function with the name of reverse_integer:- it will reverse the interger.


<?php
function reverse_integer($n) { 
 $reverse = 0; 
   while ($n > 0) {
     $reverse = $reverse * 10; 
     $reverse = $reverse + $n % 10;
     $n = (int)($n/10); 
   } 
  return $reverse; 
}

reverse_integer(27);// answer will be 72
?>

Reverse string in PHP without strrev() function:-

<?php
$string = "Techlifediary";
$count = strlen($string) - 1;
for($i = $count; $i >=0; $i--) {
   echo $string[$i];
}
?>
//Answer will be 'yraidefilhceT'
Update value of field from another table field in sql
How to Secure yourself on the Internet.