Skip to main content

How to Combine Conditions with AND (&&), OR (||) and NOT (!) in PHP

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<condition> && <condition>
<condition> || <condition>
!<condition>
PartWhat 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:

main.php, which sets signedIn to true, banned to false and age to 70, prints signedIn and not banned, age under 12 or at least 65, and not signedIn with var_dump, then sets roles to null and prints is_array of roles and in_array of admin in roles.

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​

Terminal
php main.php
Output
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:

main.php
<?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));
Terminal
php main.php
Output
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

A zsh terminal where php main.php prints bool(true) twice and bool(false) twice; for the changed file it stops with PHP Fatal error: Uncaught TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given, shown twice.

What Happened​

  • $signedIn && !$banned was true: both sides were true, because ! turned false into true.
  • $age < 12 || $age >= 65 was true: the left side was false, but the right side was true.
  • !$signedIn was false.
  • is_array($roles) && in_array('admin', $roles, true) was false, and in_array never ran. is_array(null) was false, so && already knew the answer and skipped its right side.
  • Without the is_array check, in_array got null and stopped the script with TypeError: 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, as Fatal error:.

Common Mistakes​

MistakeWhat happensDo this instead
Writing $ok = true and false;$ok is true, because = runs before andUse &&, 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 truePut 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 falseAdd parentheses
Treating the string 'false' as falseA non-empty string other than '0' is true: 'false' && true was trueCompare the string itself, as in $value === 'true'

Troubleshooting​

SymptomLikely causeFix
TypeError: in_array(): Argument #2 ($haystack) must be of type array, null givenin_array got nullPut 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 meantAdd 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​