PHP logical operators: AND, OR, XOR, NOT, and precedence

PHP logical operators interpret their operands as Boolean values and return true or false. && and and require both operands to be true. || and or require at least one. xor requires exactly one, and ! reverses the truth value of a single operand.

Logical operators and their truth table

A and B stand for conditions. and shares a truth table with &&, and or shares one with ||. Ordinary OR is also true when both conditions are true. Choose XOR when exactly one may be true. PHP Manual — Logical operators

A B A && B A || B A xor B !A
true true true true false false
true false false true true false
false true false true true true
false false false false false true

Save the first example as logical.php and run php logical.php. It prints one JSON line per combination in the order shown above. echo prints false as an empty string, so JSON or var_dump() makes the Boolean result easier to inspect. Save each example in its own file.

<?php
foreach ([[true, true], [true, false], [false, true], [false, false]] as [$a, $b]) {
    echo json_encode([
        'a' => $a, 'b' => $b,
        'and' => $a && $b, 'or' => $a || $b,
        'xor' => ($a xor $b), 'not_a' => !$a,
    ]), PHP_EOL;
}

Assignment precedence for and, or, and their symbolic forms

Two operators can have the same truth table and store different values in an assignment. && and || bind more tightly than =. and, xor, and or bind less tightly. Save the following as precedence.php and run php precedence.php.

<?php
$a = true && false;
$b = true and false;
$c = false || true;
$d = false or true;
var_dump($a, $b, $c, $d);

$both = (true and false);
$either = (false or true);
$one = (true xor false);
var_dump($both, $either, $one);

The first four values are false, true, true, false. PHP groups the second expression as ($b = true) and false, leaving true in $b. The assignment to $d also happens first, leaving it false. With parentheses around each complete expression, the last three stored values are false, true, true. PHP Manual — Operator precedence

When you mix && and ||, AND binds more tightly. For example, $hasPass || $isMember && $isOpen is true for a pass holder even when the venue is closed. If opening hours should apply to both groups, write ($hasPass || $isMember) && $isOpen.

When short-circuit evaluation skips the right operand

With && or and, a false left operand skips the right operand. With || or or, a true left operand skips it. This is short-circuit evaluation. PHP evaluates both operands of xor to find out whether exactly one is true. Save the example as short-circuit.php and run php short-circuit.php.

<?php
function checkRight(): bool
{
    echo 'Right operand evaluated.', PHP_EOL;
    return true;
}

var_dump(false && checkRight());
var_dump(true || checkRight());
var_dump(true && checkRight());
var_dump(false xor checkRight());

The first two calls print only bool(false) and bool(true). The third and fourth evaluate the right-hand function, so each prints a message followed by bool(true). If an operation must always run, perform it first and store its result before testing the condition. An if block makes intentionally conditional work explicit.

Test numbers and strings in conditions

PHP treats false, 0, 0.0, the empty string, the string "0", an empty array, and null as false in a Boolean context. The string "false" and the one-element array [0] are true. PHP Manual — Booleans

<?php
$value = '12';
if (is_numeric($value) && (float) $value > 0) {
    echo 'The value is positive.', PHP_EOL;
} else {
    echo 'The value is not a positive number.', PHP_EOL;
}

var_dump((bool) '0', (bool) 'false', (bool) [], (bool) [0]);

Save the following as numeric-input.php and run php numeric-input.php. The positive-number message is followed by false, true, false, true. With $value set to 'abc', is_numeric() fails and the conversion and comparison are skipped. '0' passes the numeric check but fails the positive-number check.

If zero is valid input for your application, if ($value) alone filters it out. Check whether a value is present, numeric, and within the allowed range separately. If the format matters, define which numeric notations you accept.