Site icon T4Tutorials.com

PHP program to swap the two numbers in PHP with form database

PHP program to swap the two numbers in PHP with form database

PHP program to swap the two numbers in PHP. 

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
$a = 10;
$b = 20;
echo "<br> The number before swapping is: <br>";
echo "<br>Number a =".$a." and b=".$b."<br>";
$c = $a;
$a = $b;
$b = $c; 
echo "<br>The number after swapping is: <br>";
echo "<br>Number a =".$a." and b=".$b."<br>";
?>

PHP program to swap the two numbers in PHP with form.

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

<?php
$n1="";
$n2="";
$n3="";
$r1="";
$r2="";
if(isset($_POST["S1"]))
{
	$n1=$_POST["T1"];
	$n2=$_POST["T2"];
	
	
	$r1=$n2;
	$r2=$n1;
	
}

?>
<html>
<body>
    <h2 align="center">Swap two numbers using a third variable</h2>

    <form method="post" name="Form1" >


        <table align="center" >
            <tr><td >Value of A</td>
				<td>= <input  name="T1" size="15"  type="text" value="<?php echo($n1);?>"></td>
			</tr>
			
            <tr>
				<td >Value of B</td><td>= <input  name="T2" size="15"    type ="text" value="<?php echo($n2);?>"> </td>
			</tr>
			            	
			<tr><td colspan="2"><p></p></td> </tr>
            
			<tr>
				<td colspan="2" align="center"> <input value="SWAP"   type="submit" name="S1"> 
						 </td>
			</tr>
			
			<tr><td colspan="2"><p></p></td> </tr>
			
			<tr><td><h2 align="center">After Swaping</h2></td></tr>
			
            <tr>
				<td >Value of A</td>
				<td>= <input  name="T4" size="15" readonly="" type="text" value="<?php echo($r1);?>"></td>
			</tr>
			
            <tr>
				<td >Value of B</td>
				<td>= <input  name="T5" size="15" readonly="" type="text" value="<?php echo($r2);?>"></td>
			</tr>
			
        </table>

    </form>

</body>
</html>

PHP program to swap the two numbers in PHP with  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.

<?php
$n1="";
$n2="";
$n3="";
$r1="";
$r2="";
if(isset($_POST["S1"]))
{
	$n1=$_POST["T1"];
	$n2=$_POST["T2"];
	
	
	$r1=$n2;
	$r2=$n1;
	
	$server="localhost";
	$name="roor";
	$password="";
	$dbname="swap";
	$con=mysqli_connect("localhost","root","","swap");
	
	$sql=mysqli_query($con,"INSERT INTO u_table (first_no,second_no,swap_1,swap_2) VALUES ('$n1','$n2','$r1','$r2')");
	echo"<h3>Values are Saved in database</h3>";
}

?>
<html>
<body>
    <h2 align="center">Swap two numbers using a third variable</h2>

    <form method="post" name="Form1" >


        <table align="center" >
            <tr><td >Value of A</td>
				<td>= <input  name="T1" size="15"  type="text" value="<?php echo($n1);?>"></td>
			</tr>
			
            <tr>
				<td >Value of B</td><td>= <input  name="T2" size="15"    type ="text" value="<?php echo($n2);?>"> </td>
			</tr>
			            	
			<tr><td colspan="2"><p></p></td> </tr>
            
			<tr>
				<td colspan="2" align="center"> <input value="SWAP"   type="submit" name="S1"> 
						 </td>
			</tr>
			
			<tr><td colspan="2"><p></p></td> </tr>
			
			<tr><td><h2 align="center">After Swaping</h2></td></tr>
			
            <tr>
				<td >Value of A</td>
				<td>= <input  name="T4" size="15" readonly="" type="text" value="<?php echo($r1);?>"></td>
			</tr>
			
            <tr>
				<td >Value of B</td>
				<td>= <input  name="T5" size="15" readonly="" type="text" value="<?php echo($r2);?>"></td>
			</tr>
			
        </table>

    </form>

</body>
</html>

Database:

Figure: swap number program in PHP.

 

Exit mobile version