PHP Operators

PHP supports various operators for performing arithmetic, comparison, logical, assignment, and other operations.

Arithmetic Operators

Arithmetic operators are used for mathematical operations.

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus (remainder)
  • ** Exponentiation

Comparison Operators

Comparison operators are used to compare values.

  • == Equal to
  • != Not equal to
  • === Identical (equal and same type)
  • !== Not identical
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to

Logical Operators

Logical operators are used for conditional statements.

  • and Logical and
  • or Logical or
  • xor Logical xor
  • ! Logical not

Assignment Operators

Assignment operators are used to assign values to variables.

  • = Assign
  • += Add and assign
  • -= Subtract and assign
  • *= Multiply and assign
  • /= Divide and assign
  • %= Modulus and assign
  • **= Exponentiation and assign

Example of PHP Operators

<?php
$a = 10;
$b = 5;

echo $a + $b . "\n";
// Outputs: 15
echo $a == $b . "\n";
// Outputs: 
echo $a > $b . "\n";
// Outputs: 1
?>

Output:

15

1

Explanation: This example demonstrates the use of arithmetic, comparison, and logical operators in PHP.