Unexpected Behavior, XOR Logical Operator - PHP

Asked

Viewed 104 times

3

why it is possible to see this behaviour of the logical operator xor?

$bool = false xor true;
var_dump($bool); // bool(false) 

$bool = true xor false;
var_dump($bool); // bool(true)

from what I read, xor should return true only if one or the other is true, but not both(exclusivity), so should not return everything boolean(true)?

1 answer

4


The first case is returning false because the operator = takes precedence over the operator xor.

$bool = false xor true; // false

Instead, do it like this:

$bool = (false xor true); // true

Source: PHP: Operator Precedence

  • Wow, then had a fight of different operators, one of assignment and another of logic, and the of assignment came out in front. uffa, now it’s clearer, thank you Rodrigo!

  • 1

    That’s right! To see how the operator xor Okay, just do one var_dump(false xor true).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.