Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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

$a xor $b

Truth Table

The evaluation logic follows strict exclusive disjunction:
$a$b$a xor $b
truetruefalse
truefalsetrue
falsetruetrue
falsefalsefalse

Implicit Boolean Casting

Before evaluation, PHP implicitly casts non-boolean operands to boolean values according to standard PHP type-juggling rules.
var_dump(1 xor 0);         // bool(true)  -> (true xor false)
var_dump("string" xor 1);  // bool(false) -> (true xor true)
var_dump("" xor null);     // bool(false) -> (false xor false)

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.
// Without parentheses: Assignment takes precedence
$result = true xor false; 
// Evaluated as: ($result = true) xor false;
var_dump($result); // bool(true)

$result = true xor true;
// Evaluated as: ($result = true) xor true;
var_dump($result); // bool(true) - Incorrect logical result for the variable

// With parentheses: XOR takes precedence
$result = (true xor true);
var_dump($result); // bool(false) - Correct logical result

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.
// Logical XOR
var_dump(3 xor 6); 
// Evaluates as: (true xor true)
// Returns: bool(false)

// Bitwise XOR
var_dump(3 ^ 6);   
// Evaluates as: 011 ^ 110 (binary)
// Returns: int(5)
Master PHP with Deep Grasping Methodology!Learn More