PHP If Operators
In this page:
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
$score = 85;
if ($score >= 60) {
echo "Pass";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
var_dump("abc" == 0);
var_dump("abc" === 0);
?>
Login to try C/C++/Java/PHP code in the editor
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
$age = 25;
$hasLicense = true;
if ($age >= 18 && $hasLicense) {
echo "Can drive";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
$value = "";
if (!$value) {
echo "Value is empty";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
$loggedIn = true and false;
var_dump($loggedIn);
?>
Login to try C/C++/Java/PHP code in the editor
- Using == (loose equality) when === (strict equality) is what you actually need, letting PHP's type juggling produce surprising true results, like "0" == false.
- 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.
- Writing overly long compound conditions without parentheses to group them clearly, making the actual logic hard to verify at a glance.
- 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.
All PHP comparison and logical operators are supported in every PHP version and work identically across all environments.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: