2

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();.

tomsseisums
  • 13,168
  • 19
  • 83
  • 145
  • Related: [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – jensgram Jun 20 '11 at 08:31

5 Answers5

6

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_]
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • True, but there's a chance that there are no spaces between `xor` comparison, i.e: `if(($this && $that)xor($foo && $bar))`. A rare occurance, but still can happen. – tomsseisums Jun 20 '11 at 08:38
5

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
    ...


Edit: After some experimentation with PHP's very lax type coercion, I give you a pseudo-operator of own creation, the ==! 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.

Casey Chu
  • 25,069
  • 10
  • 40
  • 59
  • Huh, yes, but as you said, this provides way too many search results! What would `==!` translate to? – tomsseisums Jun 20 '11 at 08:46
  • 1
    `==!` works as a logical `xor`. `$a ==! $b` is actually `$a == !$b`, which is the same as `!$a != !$b` because PHP coerces both sides to boolean when one side is boolean. – Casey Chu Jun 20 '11 at 08:55
4

See bitwise operators. It's the symbol ^.

Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • Ah, so bitwise operators == logical operators? Or there is a difference between those? – tomsseisums Jun 20 '11 at 08:31
  • @Tom there's a distinction in that one operates with booleans and the other with integers. Still, the type-lax nature of PHP allows you to use `^` for the same purpose as `xor`. – Artefacto Jun 20 '11 at 08:39
3

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.

Connman
  • 158
  • 3
0

If you're looking for logical xor, there is no symbol equivalent.

duri
  • 14,991
  • 3
  • 44
  • 49