Current Location: Home> Latest Articles> Comprehensive Guide to PHP Operators: Arithmetic, Logical, Comparison, Assignment, and Bitwise Explained

Comprehensive Guide to PHP Operators: Arithmetic, Logical, Comparison, Assignment, and Bitwise Explained

M66 2025-10-27

Overview of PHP Operators

In PHP, operators are symbols used to perform operations on variables and values. Understanding how operators work helps you write cleaner and more efficient code. PHP operators are mainly divided into arithmetic, comparison, logical, assignment, bitwise, and other special operators.

Arithmetic Operators

Arithmetic operators perform basic mathematical operations:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
$a = 10;  
$b = 3;  
echo $a + $b; // Output 13  
echo $a % $b; // Output 1

Comparison Operators

Comparison operators are used to compare two values and return a boolean result (true or false):

  • Equal (==)
  • Not equal (!=)
  • Identical (===)
  • Not identical (!==)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
$a = 5;  
$b = 10;  
var_dump($a < $b); // true

Logical Operators

Logical operators are often used in conditional statements to combine multiple expressions:

  • AND (&&)
  • OR (||)
  • NOT (!)
$a = true;  
$b = false;  
var_dump($a && $b); // false

Assignment Operators

Assignment operators assign values to variables, and can perform calculations simultaneously:

  • = Basic assignment
  • += Addition assignment
  • -= Subtraction assignment
  • *= Multiplication assignment
  • /= Division assignment
  • %= Modulus assignment
$x = 5;  
$x += 3;  
echo $x; // Output 8

Bitwise Operators

Bitwise operators perform operations on binary representations of numbers. They are useful for low-level logic or permission handling:

  • AND (&)
  • OR (|)
  • XOR (^)
  • Left shift (<<)
  • Right shift (>>)
$a = 5; // Binary 0101  
$b = 3; // Binary 0011  
echo $a & $b; // Output 1

Other Operators

Besides the main categories, PHP provides several special operators:

  • Conditional (ternary) operator (?:)
  • Increment (++x or x++)
  • Decrement (--x or x--)
  • Type casting (int, float, string, etc.)
  • Parentheses ()
  • Comma (,)
$score = 80;  
$result = ($score >= 60) ? 'Pass' : 'Fail';  
echo $result; // Output Pass

Conclusion

Mastering PHP operators is essential for understanding program logic and controlling execution flow. With practice, these operators can make your code more efficient and readable.