PHP xor returns an invalid value

Using php 7.1.0 I run this little test:

<?php

$a = true;
$b = true;

$value = $a xor $b;
if ($value == true) {
    print "bad!\n";
} else {
    print "good\n";
}    

and he comes back and speaks badly. What for? Xor of two true values ​​must be FALSE, not true.

+4
source share
1 answer

The problem is operator priority. The statement xorhas a lower priority than =, so your statement is equivalent to:

($value = $a) xor $b;

You need to write:

$value = ($a xor $b);

or

$value = $a ^ $b;

^ XOR, . true false 1 0, . , - , - XOR , , .

. PHP

. PHP bool:

+7

Source: https://habr.com/ru/post/1667522/


All Articles