Reputation: 461
My question is operator related. Today I studied about the operators. Having a confusion. In PHP what is the difference between "or" and "xor". I know both of them are related to Boolean expression. But can't find the original difference.
Anyone please help to understand it more clearly.
Upvotes: 36
Views: 90646
Reputation: 324
Another way of putting it: OR can be seen as an AND/OR connector. One or both choices can be true or chosen. XOR, on the other side, could be described as a "real" OR. Only one of both choices can be true (or chosen).
Upvotes: 9
Reputation: 73031
It has to do with mutual exclusion. xor
is exclusive. or
is inclusive.
Truth Table Comparison
$x $y ($x or $y) ($x xor $y)
0 0 0 0
1 0 1 1
0 1 1 1
1 1 1 0
Note: the difference in the last case. xor
is only true when either $x
or $y
is true, but not both (as the case for or
).
Upvotes: 75
Reputation: 815
The difference is with an input of an even amount of 1's (or true's):
true or true = true
true xor true = false
true or true or true = true
true xor true xor true = true
true or false or true = true
true xor false xor true = false
Truth table of OR:
in1 | in2 | out
-------|-------|-----
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
Truth table of XOR:
in1 | in2 | out
-------|-------|-----
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0
Upvotes: 14
Reputation: 12243
one is exclusive or -> xor, and the other is or which means at least one of the conditions are met in a truth table. Or is not mutually exclusive, xor is.
Upvotes: 2
Reputation: 2147
xor means "exclusive or". That is to say, it's or, but with the single change that if both parameters to the operation are true, the answer is false.
A xor B == (A or B) && !(A and B)
Upvotes: 20