How to Combine Conditions with AND (&&), OR (||) and NOT (!) in PHP
In PHP, && (and) is true only when both conditions are true, || (or) is true when at least one is, and ! (not) turns true into false and false into true. && and || stop as soon as they know the answer, so the right side can rely on the left.
Syntax
<condition> && <condition>
<condition> || <condition>
!<condition>
| Part | What it does |
|---|---|
&& | And: true only when both conditions are true. When the left one is false, the right one isn't evaluated. |
|| | Or: true when at least one condition is true. When the left one is true, the right one isn't evaluated. |
! | Not: turns true into false, and false into true. |
<condition> | Any expression. PHP converts its value to true or false. |
The PHP manual gives $a && $b as "true if both $a and $b are true", $a || $b as "true if either $a or $b is true", and ! $a as "true if $a is not true". Its example notes that the right side "will never get called as those operators are short-circuit" when the left side already decides the result.
The Example
The example checks three conditions about a user: whether they're signed in and not banned, whether their age gets a reduced ticket price (under 12, or 65 and over), and whether they aren't signed in. Then it checks for the admin role with in_array, but only after is_array confirms the roles are an array, because this user has no roles at all. Finally it calls in_array without that check.
Prerequisites
- PHP. This guide uses PHP 8.6.0.
Step 1: Write the Code
Create a file named main.php:

<?php
declare(strict_types=1);
$signedIn = true;
$banned = false;
$age = 70;
// && is true only when both sides are true.
var_dump($signedIn && !$banned);
// || is true when at least one side is true.
var_dump($age < 12 || $age >= 65);
// ! turns true into false, and false into true.
var_dump(!$signedIn);
$roles = null; // no roles at all
// && skips its right side when the left side is false, so in_array never gets null.
var_dump(is_array($roles) && in_array('admin', $roles, true));
var_dump prints a value with its type, so true shows as bool(true). The third argument of in_array, true, makes it compare types as well as values.
Step 2: Run It
php main.php
bool(true)
bool(true)
bool(false)
bool(false)
The command is the same in zsh, bash and PowerShell.
Step 3: Remove the is_array Check
Replace main.php with a version that calls in_array on its own:
<?php
declare(strict_types=1);
$roles = null; // no roles at all
// Without the is_array check, in_array gets null.
var_dump(in_array('admin', $roles, true));
php main.php
PHP Fatal error: Uncaught TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given in /Users/Shared/you/main.php:7
Stack trace:
#0 /Users/Shared/you/main.php(7): in_array('admin', NULL, true)
#1 {main}
thrown in /Users/Shared/you/main.php on line 7
Fatal error: Uncaught TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given in /Users/Shared/you/main.php:7
Stack trace:
#0 /Users/Shared/you/main.php(7): in_array('admin', NULL, true)
#1 {main}
thrown in /Users/Shared/you/main.php on line 7

What Happened
$signedIn && !$bannedwastrue: both sides weretrue, because!turnedfalseintotrue.$age < 12 || $age >= 65wastrue: the left side wasfalse, but the right side wastrue.!$signedInwasfalse.is_array($roles) && in_array('admin', $roles, true)wasfalse, andin_arraynever ran.is_array(null)wasfalse, so&&already knew the answer and skipped its right side.- Without the
is_arraycheck,in_arraygotnulland stopped the script withTypeError: in_array(): Argument #2 ($haystack) must be of type array, null given. - The error appears twice because this PHP's settings both log errors to the terminal, as
PHP Fatal error:, and display them, asFatal error:.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing $ok = true and false; | $ok is true, because = runs before and | Use &&, or put the and in parentheses |
Writing !$age >= 65 to mean "not 65 or over" | ! applies to $age first: for an age of 30, it gave false, while !($age >= 65) gave true | Put the comparison in parentheses |
Mixing && and || without parentheses | && runs first: with $a set to true and $b and $c to false, $a || $b && $c was true, but ($a || $b) && $c was false | Add parentheses |
Treating the string 'false' as false | A non-empty string other than '0' is true: 'false' && true was true | Compare the string itself, as in $value === 'true' |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given | in_array got null | Put is_array($roles) && in front of it |
A condition that's always true or always false | !, and or or grouped the parts differently than you meant | Add parentheses |
FAQ
Which values count as false?
The PHP manual lists the values that convert to false. They include false, the integer 0, the floats 0.0 and -0.0, the empty string "" and the string "0", an empty array, and null. "Every other value is considered true". In my check, '0' && true, '' || false, [] || false and 0.0 || false were all false.
Are and and or the same as && and ||?
They give the same results, but they have lower precedence, lower even than =. The PHP manual: "The reason for the two different variations of "and" and "or" operators is that they operate at different precedences." That's why $ok = true and false; left $ok set to true.
Conclusion
&&, || and ! combine conditions into one true or false. Because && and || skip their right side once the answer is known, put a type check such as is_array on the left of the code that relies on it, and use parentheses wherever the grouping isn't obvious.
Additional Resources
- Logical operators, in the PHP manual.
- Operator precedence, in the PHP manual.
- Booleans, in the PHP manual, with the values that convert to
false.
