Search code examples
phpxor

xor symbol representation?


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


Solution

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