1 min read

PHP Operators

Operators are used to perform operations on variables and values.


1. Arithmetic Operators

Used with numeric values to perform common arithmetical operations.

  • +: Addition ($x + $y)
  • -: Subtraction ($x - $y)
  • *: Multiplication ($x * $y)
  • /: Division ($x / $y)
  • %: Modulus (remainder of $x divided by $y)
  • **: Exponentiation ($x ** $y)
$x = 10;
$y = 3;
echo $x % $y; // Outputs: 1

2. Comparison Operators

Used to compare two values (number or string).

  • ==: Equal (value only)
  • ===: Identical (value and type)
  • != or <>: Not equal
  • !==: Not identical
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to
  • <=>: Spaceship (Returns -1, 0, or 1)
$x = 100;
$y = "100";

var_dump($x == $y); // true
var_dump($x === $y); // false

3. Logical Operators

Used to combine conditional statements.

  • and / &&: True if both are true.
  • or / ||: True if either is true.
  • xor: True if either is true, but not both.
  • !: True if not true.

4. String Operators

PHP has two operators that are specifically designed for strings.

  • .: Concatenation
  • .=: Concatenation assignment

programming/php/php