Skip to content

PHP Operators

Operators perform operations on variables and values.


Compare two values and return a boolean result.

OperatorNameExampleResult
==Equal5 == "5"true (type coercion)
===Identical5 === "5"false (strict)
!=Not equal5 != 3true
!==Not identical5 !== "5"true (strict)
>Greater than10 > 5true
<Less than3 < 7true
>=Greater or equal5 >= 5true
<=Less or equal4 <= 3false
$a = 10;
$b = "10";
$a == $b // true (values equal, types ignored)
$a === $b // false (different types)
// Common type coercion cases
0 == false // true
0 === false // false
"0" == 0 // true
"0" === 0 // false

Combine multiple conditions.

OperatorNameDescription
&& or andANDBoth conditions must be true
|| or orORAt least one must be true
!NOTReverses the condition
xorXORExactly one must be true
// AND - both must be true
if ($age >= 21 && $income >= 30000) {
echo "Approved";
}
// OR - at least one must be true
if ($age >= 65 || $income < 20000) {
echo "Eligible for discount";
}
// NOT - reverses boolean
if (!$isLoggedIn) {
echo "Please login";
}
// XOR - exactly one must be true
if ($useCreditCard xor $usePayPal) {
echo "Payment method selected";
}

Operator precedence: !&&||. Use parentheses for clarity.


OperatorNameExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiplication4 * 312
/Division15 / 35
%Modulus10 % 31
**Exponentiation2 ** 38
$total = $price * $quantity - $discount;
$isEven = ($number % 2 == 0);

OperatorExampleEquivalent
=$a = 5Assign
+=$a += 3$a = $a + 3
-=$a -= 2$a = $a - 2
*=$a *= 4$a = $a * 4
/=$a /= 2$a = $a / 2
.=$a .= "x"$a = $a . "x"
$total = 0;
$total += 50; // 50
$message = "Hi";
$message .= "!"; // "Hi!"

OperatorDescription
++$aPre-increment (increment, then return)
$a++Post-increment (return, then increment)
--$aPre-decrement
$a--Post-decrement
$count = 5;
echo $count++; // 5, then becomes 6
echo ++$count; // 7, then outputs 7

OperatorNameExample
.Concatenation"Hi" . " there"
.=Append$a .= " more"
$name = $firstName . " " . $lastName;
$html = "<div>";
$html .= "<p>Content</p>";
$html .= "</div>";

  1. Use strict comparison (===) unless you need type coercion.
  2. Use parentheses when combining multiple logical operators for clarity.
  3. Use compound operators (+=, -=, .=) for cleaner code.
  4. Be aware of type coercion with == vs ===.