W3docs

xor

The xor keyword in PHP is used as a logical operator that returns TRUE if one of the two expressions is TRUE and the other is FALSE. If both expressions are

Introduction

The xor keyword in PHP is the logical exclusive-or operator. It returns TRUE only when exactly one of its two operands is TRUE and the other is FALSE. If both operands are TRUE, or both are FALSE, then xor returns FALSE. This is the key difference from the or operator, which returns TRUE whenever either or both operands are TRUE.

In plain terms: xor answers the question "is one of these true, but not both?"

This page covers the truth table for xor, a runnable example, its precedence (and the common assignment gotcha it causes), and when reaching for xor actually makes sense.

Truth table

The behavior of xor is fully described by these four cases:

$a$b$a xor $b
TRUETRUEFALSE
TRUEFALSETRUE
FALSETRUETRUE
FALSEFALSEFALSE

Notice the result is TRUE only on the two "mixed" rows — that is what makes it exclusive.

Example

Here's an example that demonstrates the use of xor in PHP:

Example of PHP xor keyword

php— editable, runs on the server

In the example above, the if statement will evaluate to TRUE because $x > 0 is TRUE and $y > 10 is FALSE. Therefore, the output of the code will be: "One of the expressions is true and the other is false".

It's worth noting that xor has a lower precedence than the and and or operators, so it's important to use parentheses to group expressions properly if needed.

Precedence and the assignment gotcha

The word operators and, or, and xor all have lower precedence than the assignment operator (=). This trips people up: in $result = true xor false, the assignment binds first, so PHP evaluates it as ($result = true) xor false. The xor is applied after the value has already been assigned, and its result is thrown away.

<?php

// = binds tighter than xor, so this is ($result = true) xor false
$result = true xor false;
var_dump($result); // bool(true)  — the xor never affects $result

// Use parentheses to get the value you expect
$result = (true xor false);
var_dump($result); // bool(true)

$result = (true xor true);
var_dump($result); // bool(false)

The rule of thumb: when you want the result of an xor expression stored in a variable, wrap the expression in parentheses. If you only need xor inside an if condition (as in the example above), the issue does not arise.

For the full precedence table and the symbol operators (&&, ||, !), see PHP Operators.

More Information

Here's some more information about the xor operator in PHP:

  • The xor operator is also called the "exclusive or" operator because it returns TRUE only if one of the expressions is TRUE and the other is FALSE, but not if both are TRUE or both are FALSE.
  • The xor operator automatically converts its operands to boolean values before evaluation, following PHP's standard type juggling rules.
  • The xor operator can be useful in situations where you need to check that only one of two conditions is true, but not both. For example, if you have two checkboxes on a form and you want to make sure that the user selects only one of them, you can use xor to validate the input.
  • Unlike and and or, the xor operator does not short-circuit; both operands are always evaluated regardless of the first operand's value.
  • The xor operator can be combined with other operators such as == or != to create more complex expressions. For example, you can use ($x == $y) xor ($a != $b) to check that either $x is equal to $y or $a is not equal to $b, but not both. Such expressions are most often placed inside an if statement.
  • Note that xor is a logical operator. For bitwise XOR operations (toggling individual bits of an integer), use the ^ operator instead.
  • The related word operators are and and or. PHP also provides the symbol forms && and ||, which have higher precedence than their word counterparts.
  • It's worth noting that the xor operator is not commonly used in PHP programming, and you may find that you can achieve the same results using other operators or control structures. However, it can be a useful tool in certain situations where you need to perform logical operations on Boolean values.

Practice

Practice
What is the meaning of the XOR (Exclusive OR) logical operator in PHP?
What is the meaning of the XOR (Exclusive OR) logical operator in PHP?
Was this page helpful?