Swapping Using Third Variable:

Swap two no a=10 and b = 20 Using third Variable

<?php
$a = 10;
$b = 20;
// Swapping Logic  
$third = $a;  
$a = $b;  
$b = $third;  
echo "After swapping result:<br><br>";  
echo "a =".$a."  b=".$b;  

?>

Output:

After swapping result:

a = 20  b=10

Swapping Without using Third Variable:

Swapping two numbers without using a third variable can be done in several ways. 

Here are three common methods:

  1. Using Addition and Subtraction
  2. Using Multiplication and Division
  3. Using Bitwise XOR Operation

1.Using Addition and Subtraction

<?php  
$a=10;  
$b=20;  
$a=$a+$b;  
$b=$a-$b;  
$a=$a-$b;  
echo "Value of a: $a</br>";  
echo "Value of b: $b</br>";  
?>  

2.Using Multiplication and Division

<?php  
$a=10;  
$b=20;  
$a = $a * $b;
$b = $a / $b;
$a = $a / $b;  
echo "Value of a: $a</br>";  
echo "Value of b: $b</br>";  
?>  

3.Using Bitwise XOR Operation

<?php  
$a=10;  
$b=20;  
$a = $a ^ $b;
$b = $a ^ $b;
$a = $a ^ $b;  
echo "Value of a: $a</br>";  
echo "Value of b: $b</br>";  
?>  

Output:

Value of a: 20
Value of b: 10