Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

PHP Program to print the numbers in reverse order with form and database

PHP Program to print the numbers in reverse order with form and database

In this tutorial, we will learn about the followings;

  1. PHP Program to print the numbers in reverse order
  2. PHP Program to print the numbers in reverse order with the form 
  3. PHP Program to print the numbers in reverse order with database

PHP Program to print the numbers in reverse order.

In this example, programmer stores the values in the variables. However, if you interested to see the program about taking values in the form, please see example 2.

<?php

$number=123;
$orignal_num=$number;
$rs=0;
while($number>0)
{
$rs = $rs * 10 + $number % 10;
$number = (int)($number / 10);

}
print_r("Result:$orignal_num<br>Revrse of Number is= $rs");

?>

PHP Program to print the numbers in reverse order with the form 

In this example, we will take the inputs from the user and store them in variables.

<html>
<head>
<title></title>
   
</head>
<body>

                     <div class="nukr"><h2 class="col_insert" >Reverse Number</h2></div>
                     <form action="" method="post" name="insert_form" id="insert_form" class="comment-form">
                     <table class="tableI1" align="left" border="0" bordercolor="#FFFFFF" >
                     <tr><td ><b><i>Enter Number</b></i></td>
                     <td><input id="Charge" name="Charge" type="text" size="30" /> 
                     <tr><td></td><td><input type="submit" id="btnsubmit" name="btnsubmit" value="Calculate" size="30"/></td></tr>
                     <tr><td></td><td>       </td></tr>
                     </table>
                      </form>

</body>
</html>
<?php
if(isset($_POST['btnsubmit']))
{
$number=$_POST['Charge'];
$orignal_num=$number;
$rs=0;
while($number>0)
{
$rs = $rs * 10 + $number % 10;
$number = (int)($number / 10);

}
print_r("Result:$orignal_num<br>Revrse of Number is= $rs");
}


?>

PHP Program to print the numbers in reverse order with the database.

In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.

<html>
<head>
<title></title>
   
</head>
<?php
include 'dbconction.php';
?>
<body>

                     <div class="nukr"><h2 class="col_insert" >Reverse Number</h2></div>
                     <form action="" method="post" name="insert_form" id="insert_form" class="comment-form">
                     <table class="tableI1" align="left" border="0" bordercolor="#FFFFFF" >
                     <tr><td ><b><i>Enter Number</b></i></td>
                     <td><input id="Charge" name="Charge" type="text" size="30" /> 
                     <tr><td></td><td><input type="submit" id="btnsubmit" name="btnsubmit" value="Calculate" size="30"/></td></tr>
                     <tr><td></td><td>       </td></tr>
                     </table>
                      </form>

</body>
</html>
<?php
if(isset($_POST['btnsubmit']))
{
$number=$_POST['Charge'];
$orignal_num=$number;
$rs=0;
while($number>0)
{
$rs = $rs * 10 + $number % 10;
$number = (int)($number / 10);

}
$sql= "insert into reves(Number, Revers)
Value ('$orignal_num','$rs')";
   if($conn->query($sql)=== TRUE)
{
	echo"<font color=#FF00FF> New Record Inserted<br></font>";
	}
  else
  {
	  echo"Error:".$sql."<br>".$conn->error;
	  }	
print_r("Youer Input=$orignal_num<br>Revrse of Number is= $rs");
}


?>

dbconnection.php

<?php
error_reporting(0);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "fyp";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
else
{   
	}
//echo "Connected successfully";
mysql_query("set character_set_results='utf8'"); 
?>

Database to print the numbers in reverse order

reverse number program in php form database

Figure: reverse number program in PHP form database.

Prof. Fzal Rehman Shamil
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325