150. PHP Program to find the Reverse of given numbers using recursion.
How does this program work?
- This program is used to find the Reverse of given numbers using recursion in PHP.
- By using recursion we can find the reverse of given numbers.
Here is the code
<html>
<head>
<title>PHP Program To find the Reverse the given numbers recursively</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num" value="" placeholder="Enter number"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
$rev_num = 0;
$base_pos = 1;
//Recursive function to find reverse of any number
function reversDigits($num)
{
global $rev_num;
global $base_pos;
if($num > 0)
{
reversDigits((int)($num / 10));
$rev_num +=($num % 10) * $base_pos;
$base_pos *=10;
}
return $rev_num;
}
if(isset($_POST[ 'submit'])) {
$number = $_POST[ 'num'];
$n1 = $number;
echo "Reverse of given number $n1 is :".reversDigits($number);
}
?>
</body>
</html>