yes i love technology

PHP: XOR swap

March 26th, 2007 by Pete

The XOR swap lets you swap the value of integer variables without using a temporary variable, it's also slightly faster.

PHP:
  1. $x ^= $y;
  2. $y ^= $x;
  3. $x ^= $y;

I can't actually think of a good reason Why you'd want to use this in PHP since memory isn't normally a big issue, however it might be good for showing off ;-) .

// Pete Graham xXx

Posted in Uncategorized, Website Development, php, computer science |

2 Responses

  1. Optimus Pete » Blog Archive » PHP: Swap Variables One Liner Says:

    […] Much like the PHP XOR swap there’s no real reason this is any better than using a temporary variable, but you might be able to impress chicks with it or something. […]

  2. TBog Says:

    $x^=$y^=$x^=$y;

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.