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.
1 2 3 4 5 6 7 8 9 10 11 |
<?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<?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: