Operators in PHP

Arithmetic operators

  • addition+
  • subtraction ‘ – ‘
  • multiplication*
  • division ‘ / ‘
  • modulo ‘ % ‘
  • exponentiation '**' 

Comparison operators

  • Value is equal to: ‘ == ‘
  • Data type and value is equal to: ‘ === ‘
  • Not equal to: ‘ != ‘ or ‘ <> ‘
  • Not identical to: ‘ !== ‘

Logical operators

Logical Operators
Operator Example Name (meaning) Result
and ( && ) $a and $b AND true if both $a and $b are true.
or ( || ) $a or $b OR true if either $a or $b is true.
Xor $a xor $b XOR true if either $a or $b is true, but not both.
! ! $a NOT true if $a is not true.

Assignment operators

  • ‘ = ‘ just assigns value e.g $name = ‘Heiki’ or $age = 10;
  • ‘ += 3 ‘ adds 3 to present value of that variable, e.g $age = 10; echo $age += 3;
  • ” .= ‘Sooneste’ “, this is string add operator e.g $name = ‘Heiki’; echo $name .= ‘Sooneste’;
  • ‘ -= ‘ subtract operator
  • ‘ *= ‘ muliply operator
  • ‘ /= ‘ divide operator
  • ‘ %= ‘ modulo operator

There are ‘PRE’ and ‘POST’ operators as well.

$a = 5;
$b = ++$a // in this case 'PRE' is used and value of $b will be 6 and $a will also $b 6

BUT if it's 'POST' operator, then things are different cos it will not be calculated right away.

$a = 5;
$b = $a++; // in this case $b will be 5, but $a will be 6