We have to use the bellow mentioned syntax to swap two number using PHP, with or without using the third variable.

Example

                                        
Without using third variable

<?php echo "Before Swapping:";
$a = 1;
$b = 2;
echo "a = $a<br>";
echo "b = $b<br>";

echo "After swapping:<br>";

$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
echo "a = $a<br>";
echo "b = $b";
?>

With using third variable

<?php echo "Before Swapping:<br>";
$a = 1;
$b = 2;
echo "a = $a<br>";
echo "b = $b<br>";

echo "After swapping:<br>";
$temp = $a;
$a = $b;
$b = $temp;

echo "a = $a<br>";
echo "b = $b<br>";
?>

Suggest An Answer

No suggestions Available!