DEV Community

Discussion on: How can you swap two variables without using a third?

Collapse
 
robencom profile image
robencom • Edited

I wrote the code based on the title only :D, i.e. swapping any 2 variables' values (any type) without using a third one:

Having $x and $y:

$x .= "-" . $y;
$y = explode("-", $x)[0];
$x = explode("-", $x)[1];

Collapse
 
amexboy profile image
Amanu

You only have 8 bytes remember?, this string manipulation takes more than that