It is very rare that I have seen some code that employs the PHP **XOR** logical operator. It is not the case that it is not required or programmers are unfamiliar with it; it probably seems that most programmers have become comfortable with using the more common ‘&&’ and ‘||’ operators to construct all logical comparisons; the XOR operator relegated to the subconscious black hole.

Take the following artificial example. We want to check if only one of the directories – either ‘$dir1’ or ‘$dir2’, but not both exists. The code using only the logical ‘&&’ , ‘||’ and ‘!’ operators is shown below.

```
```

The code obviously looks confusing, which we could easily simplify using the ‘XOR’ operator as given below. The code is concise, easier to understand, and the chances of introducing bugs is smaller than the previous version.

```
```

How is XOR any different to the “not equals” operator? Your example is the same as :

if(is_dir($dir1) != is_dir($dir2)) {

xor and != are the same for boolean expressions.

I don’t know if the two have different precedence in PHP.

interesting discussion. what happens here?

if($a AND $b XOR $c AND $d){

is it the same as this?

if(($a AND $b) != ($c AND $d)){

or this?

if($a AND ($b != ($c AND $d))){

They all look alike, but the best way is to use truth tables to prove their equivalence.