Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Created February 3, 2016 05:01
Show Gist options
  • Save hsleonis/c51443713edaae815fc5 to your computer and use it in GitHub Desktop.
Save hsleonis/c51443713edaae815fc5 to your computer and use it in GitHub Desktop.
Swap variables in PHP
<?php
// Way 1
$a = $b + $a - ($b = $a);
// Way 2
list($b, $a) = array($a, $b);
// Way 3
function swap(&$a, &$b) {
$tmp = $a;
$a = $b;
$b = $tmp;
}
// Way 4
$a ^= $b ^= $a ^= $b;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment