Write a program to swap two numbers without using third variable in PHP?
You can two numbers without using the third variable in PHP as follows:
BY Best Interview Question ON 26 Jan 2020
Example
//swap two numbers without using third variable
$x=20;
$y=10;
//add x and y, store in x i.e : 30
$x=$x+$y;
//subtract 10 from 30 i.e : 20 and store in y
$y=$x-$y;
//subtract 20 from 30 i.e : 10 and store in x
$x=$x-$y;
//now print the reversed values
echo "Now x contains : ". $x;
echo "and y contains : ". $y;
//And you can make use of some predefined functions as well while not using the third variable. This can be done as follows:
$a = 10;
$b = 20;
list($a, $b) = array($b, $a);
echo $a . " " . $b;