+ 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???

29th May 2018, 3:07 AM
Gustave A C/D C ☢️ 🛸🛸🛸
Gustave A C/D C ☢️ 🛸🛸🛸 - avatar
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
13th Feb 2020, 10:39 AM
Tibor Santa
Tibor Santa - avatar