Php to swap two variables using call by reference (function)

 

Create form for inputting numbers

(swapr.html)

<html>

<form method="POST" action="swapr.php">

Enter first number:<input type="text" name="n1">

Enter second number:<input type="text" name="n2">

<input type="submit">

</form>

</html>


Embed the php file into form.

(swapr.php)

<?php

$a=$_POST["n1"];

$b=$_POST["n2"];

 function swap(&$x,&$y) {

   $t=$x;

   $x=$y;

   $y=$t;

   echo $x,"<br>";

   echo $y,"<br>";

}

swap($a,$b);

echo $a,"<br>";

echo $b,"<br>";

?>




Comments