There are two methods for swapping:
- By using third variable.
- Without using third variable.
";
echo "a =".$a." b=".$b."
";
$third = $a;
$a = $b;
$b = $third;
echo "After swapping:
";
echo "a =".$a." b=".$b;
?>
![](https://educatech.in/wp-content/uploads/2023/07/Screen-Shot-2019-10-14-at-12.30.24-PM-e1571036632972.png)
Swapping Without using Third Variable
Swap two numbers without using a third variable is done in two ways:
- Using arithmetic operation + and
- Using arithmetic operation * and
- Using arithmetic operation + and –
";
echo "Value of a: $a";
echo "Value of b: $b";
$a=$a+$b;
$b=$a-$b;
$a=$a-$b;
echo " After Swapping:
";
echo "Value of a: $a";
echo "Value of b: $b";
?>
![](https://educatech.in/wp-content/uploads/2023/07/Screen-Shot-2019-10-14-at-12.44.06-PM-e1571037407590.png)
Using arithmetic operation * and /
";
echo "Value of a: $a";
echo "Value of b: $b";
$a=$a*$b;
$b=$a/$b;
$a=$a/$b;
echo " After Swapping:
";
echo "Value of a: $a";
echo "Value of b: $b"; ?>
![](https://educatech.in/wp-content/uploads/2023/07/Screen-Shot-2019-10-14-at-12.48.29-PM-e1571037732972.png)