PHP If Operators
In this page:
if ($a == $b && $c > $d) {
// comparison operators combined with && || !
}
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.
Example: Comparison Operators in Conditions
<?php
// Declare `$score`, set to `85`
$score = 85;
// Check whether `$score >= 60`
if ($score >= 60) {
// Print "Pass" to the output
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.
Example: Strict vs Loose Equality
<?php
// Print a detailed dump (with types) of `"abc" == 0`
var_dump("abc" == 0);
// Print a detailed dump (with types) of `"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.
Example: Combining Conditions with Logical Operators
<?php
// Declare `$age`, set to `25`
$age = 25;
// Declare `$hasLicense`, set to `true`
$hasLicense = true;
// Check whether `$age >= 18 && $hasLicense`
if ($age >= 18 && $hasLicense) {
// Print "Can drive" to the output
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.
Example: Negating a Condition with !
<?php
// Declare `$value`, set to ""
$value = "";
// Check whether `!$value`
if (!$value) {
// Print "Value is empty" to the output
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.
Example: Word Operators: and, or, not
<?php
// Declare `$loggedIn`, set to `true and false`
$loggedIn = true and false;
// Print a detailed dump (with types) of `$loggedIn`
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: