+ 3
php code question
<?php $x = "a"; $y = "b"; $x ^= $y; $y ^= $x; $x ^= $y; echo $x; //Got b echo $y; //Got a ?> why it gives that results ; does "a" and "b" have an particular value???
1 Respuesta
0
"a" and "b" have a numeric value associated with their character position in the ASCII table: actually 97 and 98.
you can verify with: echo ord($x);
The ^= operator assigns the Binary XOR (exclusive or) result of the two values.
This is a well known "hack" to swap two numbers, you can read more about how it works:
https://en.wikipedia.org/wiki/XOR_swap_algorithm