Operators are used to perform operations on variables and values. PHP supports several types of operators:

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

  • Addition (+):
  $sum = 5 + 3; // 8
  
  • Subtraction (-):
  $difference = 5 - 3; // 2
  
  • Multiplication (*):
  $product = 5 * 3; // 15
  
  • Division (/):
  $quotient = 5 / 3; // 1.6667
  
  • Modulus (%):
  $remainder = 5 % 3; // 2
  
  • Exponentiation (**):
  $power = 5 ** 3; // 125
  

Comparison Operators

Comparison operators are used to compare values and return a boolean result.

  • Equal to (==):
  $result = (5 == 3); // false
  
  • Not equal to (!=):
  $result = (5 != 3); // true
  
  • Identical (===):
  $result = (5 === '5'); // false
  
  • Not identical (!==):
  $result = (5 !== '5'); // true
  
  • Greater than (>):
  $result = (5 > 3); // true
  
  • Less than (<):
  $result = (5 < 3); // false
  
  • Greater than or equal to (>=):
  $result = (5 >= 3); // true
  
  • Less than or equal to (<=):
  $result = (5 <= 3); // false
  

Logical Operators

Logical operators are used to combine conditional statements.

  • AND (&& or and):
  $result = (true && false); // false
  
  • OR (|| or or):
  $result = (true || false); // true
  
  • XOR (xor):
  $result = (true xor false); // true
  
  • NOT (!):
  $result = !true; // false
  

Assignment Operators

Assignment operators are used to assign values to variables, often with arithmetic operations.

  • Assignment (=):
  $x = 5;
  
  • Addition assignment (+=):
  $x += 3; // equivalent to $x = $x + 3;
  
  • Subtraction assignment (-=):
  $x -= 3; // equivalent to $x = $x - 3;
  
  • Multiplication assignment (*=):
  $x *= 3; // equivalent to $x = $x * 3;
  
  • Division assignment (/=):
  $x /= 3; // equivalent to $x = $x / 3;
  
  • Modulus assignment (%=):
  $x %= 3; // equivalent to $x = $x % 3;
  

Increment/Decrement Operators

Increment and decrement operators are used to increase or decrease a variable’s value by one.

  • Increment (++):
  $x = 5;
$x++; // 6
// or
++$x; // 6
  
  • Decrement (–):
  $x = 5;
$x--; // 4
// or
--$x; // 4
  

These operators are fundamental to manipulating data and controlling the flow of a PHP application.