Reputation: 13367
PHP manual doesn't say that there is one, but... maybe there's a hidden one?
Simply, I like using if
's with symbolic operation representations, that way I'm sure that when searching through a document, I'll always find what I'm looking for. But with xor
there's possibility that my code contains such a string/value or something. Like, method in template class - exorcize();.
Upvotes: 2
Views: 3098
Reputation: 881563
The exclusive-or operator in PHP is ^
.
If you want to use xor
(in any language, really, not just PHP) but you're worried about searches for it finding things like exorcize
, you could just search for " xor "
(with a space on either side).
Or, any regular expression that prevents the surrounding text from being part of the xor
word, such as (example only):
[^a-zA-Z0-9_]xor[^a-zA-Z0-9_]
Upvotes: 6
Reputation: 25463
You could use !=
if they're both already booleans.
if (true != false) // xor
...
Of course, this has the same issue with getting false positives in your search results, so perhaps you can adopt a personal convention (at the risk of confusing your co-workers) of using the other PHP not-equal operator:
if (true <> false) // xor
...
If they're not already booleans, you'll need to cast them.
if (!$a <> !$b) // xor
...
==!
operator! $a ==! $b
is equivalent to $a xor $b
.
if ('hello' ==! 0) // equivalent to ('hello' xor 0)
...
This is actually 'hello' == !0
> 'hello' == true
> true == true
> true
, which is why it works as xor
. This works because when PHP compares one boolean with anything else, it converts the second argument to boolean too.
Upvotes: 5
Reputation: 158
If you ever find yourself in a situation where the xor operator doesn't exist then you can simulate it with and, or, and not.
(A && !B) || (!A && B) Is logically equivalent to xor.
Upvotes: 3