Skip to main content
The xor (exclusive OR) operator is a logical operator in PHP that evaluates to true if and only if exactly one of its operands evaluates to true. If both operands evaluate to true, or both evaluate to false, the expression returns false.

Syntax

Truth Table

The evaluation logic follows strict exclusive disjunction:

Implicit Boolean Casting

Before evaluation, PHP implicitly casts non-boolean operands to boolean values according to standard PHP type-juggling rules.

Operator Precedence

A critical mechanical detail of the xor operator is its position in the operator precedence table. xor has a lower precedence than the assignment operator (=). This requires explicit grouping via parentheses when assigning the result of an xor operation to a variable.

Logical xor vs. Bitwise ^

The xor operator is strictly a logical operator returning a boolean. It should not be confused with the caret symbol (^), which is the bitwise XOR operator. The bitwise operator evaluates the operands at the binary level and returns an integer or string, whereas the logical xor evaluates truthiness.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More