PHP program to swap two numbers

PHP program to swap two numbers:

The below program is to swap two numbers with and without using third variable. The PHP echo statement is used to output the result on the screen. Swapping two numbers simply means interchanging the values of two numeric variables.

Before Swapping,

A = n1

B = n2

After Swapping,

A = n2

B = n1

Using Third Variable:
Example

<!DOCTYPE html>
<html>
<body>
 
<?php
 
echo "Before Swapping:<br>";
$a = 1;  
$b = 2;
echo "a = $a<br>";
echo "b = $b<br><br>";
 
echo "After swapping:<br>";  
$temp = $a;  
$a = $b;  
$b = $temp;  
 
echo "a = $a<br>";
echo "b = $b<br>";
?> 
 
</body>
</html>

Output

Before Swapping:
a = 1
b = 2
 
After swapping:
a = 2
b = 1

Without Using Third Variable:
Example

<!DOCTYPE html>
<html>
<body>
 
<?php  
echo "Before Swapping:<br>";
$a = 1;  
$b = 2;
echo "a = $a<br>";
echo "b = $b<br><br>";
 
echo "After swapping:<br>";  
 
$a = $a + $b;  
$b = $a - $b;  
$a = $a - $b;  
 
echo "a = $a<br>";
echo "b = $b";
?> 
 
</body>
</html>

Output

Before Swapping:
a = 1
b = 2
 
After swapping:
a = 2
b = 1
Please Share