← Back to PHP Course | Chapter 4: Control Flow | Lesson 3 of 14

PHP If Operators

An if statement is only as useful as the condition inside it -- and PHP gives you a full set of comparison and logical operators to build conditions ranging from a simple equality check to a complex combination of several rules joined with AND/OR logic. Knowing these operators well is what turns a plain if into a precise decision-making tool.

Comparison Operators in Conditions

Comparison operators like ==, !=, <, >, <=, and >= each produce a true or false result by comparing two values, and that result is exactly what an if statement needs to decide which branch to run.

Note: Read a condition like $age >= 18 as a full sentence -- "is age greater than or equal to 18" -- to sanity-check the logic before running it.

Warning: A single = inside an if condition (assignment) instead of == or === (comparison) is a classic bug -- it assigns a value and evaluates as truthy, rather than comparing anything.

Example: Comparison Operators in Conditions

php
<?php
$score = 85;
if ($score >= 60) {
    echo "Pass";
}
?>

Strict vs Loose Equality

== (loose equality) converts both sides to a common type before comparing, which can produce surprising results like "abc" == 0 being true in older PHP versions. === (strict equality) instead requires both the value AND the type to match exactly, avoiding that kind of type-coercion surprise entirely.

Note: Default to === and !== in new code; reach for == or != only when you deliberately want PHP's type-juggling behavior.

Warning: Comparing a string to a number with == relies on PHP's type-conversion rules, which have changed between PHP versions -- === sidesteps this instability entirely.

Example: Strict vs Loose Equality

php
<?php
var_dump("abc" == 0);
var_dump("abc" === 0);
?>

Combining Conditions with Logical Operators

&& (AND) requires both sides to be true, || (OR) requires at least one side to be true, and ! (NOT) inverts a single condition -- these let you build a single if statement that checks several rules at once instead of nesting many separate if statements.

Note: Use parentheses around grouped sub-conditions in a complex logical expression, even when not strictly required, to make the intended grouping obvious to readers.

Warning: PHP short-circuits && and || -- if the left side of && is false, the right side is never evaluated at all, which matters if that right side has a side effect like calling a function.

Example: Combining Conditions with Logical Operators

php
<?php
$age = 25;
$hasLicense = true;
if ($age >= 18 && $hasLicense) {
    echo "Can drive";
}
?>

Negating a Condition with !

The ! operator flips a boolean value: ! true becomes false, and ! false becomes true. Wrapping a condition in ! is useful for checking "the opposite" of something, like whether a variable is NOT empty or a value does NOT match a target.

Note: Use !empty($var) or !is_null($var) style checks when the "negative" phrasing reads more naturally than restructuring the whole condition.

Warning: Stacking multiple ! operators, or negating an already-complex compound condition, can make logic hard to read -- consider restructuring instead.

Example: Negating a Condition with !

php
<?php
$value = "";
if (!$value) {
    echo "Value is empty";
}
?>

Word Operators: and, or, not

PHP also offers word-form logical operators -- and, or, not -- which behave almost like &&, ||, and !, but with much lower operator precedence. This low precedence can cause surprising bugs when mixed with the assignment operator =, so most style guides recommend sticking to the symbolic forms (&&, ||, !) in ordinary conditions.

Note: Prefer &&, ||, and ! over and, or, not in everyday conditions, reserving the word forms for the rare cases where their specific low precedence is intentionally useful.

Warning: Because and/or have lower precedence than =, code like $result = false or true; actually assigns false to $result first, then evaluates or true separately -- a frequent source of confusion.

Example: Word Operators: and, or, not

php
<?php
$loggedIn = true and false;
var_dump($loggedIn);
?>
Common Mistakes
  1. Using == (loose equality) when === (strict equality) is what you actually need, letting PHP's type juggling produce surprising true results, like "0" == false.
  2. Confusing && (logical AND) with & (bitwise AND) -- they look similar but do completely different things, and using the wrong one inside an if silently produces wrong results.
  3. Writing overly long compound conditions without parentheses to group them clearly, making the actual logic hard to verify at a glance.
Chapter Summary
  • Comparison operators (==, ===, !=, !==, <, >, <=, >=) build the individual true/false checks used inside an if condition.
  • Logical operators (&&, ||, !, and their word forms and/or/not) combine multiple comparisons into one overall condition.
  • === and !== check both value and type, avoiding the surprising type-coercion results that == and != can produce.
Browser Support

All PHP comparison and logical operators are supported in every PHP version and work identically across all environments.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.