Operators

Let us understand the below terms before we get into more details.

1. Operator

An operator is a symbol which has special meaning and performs an operation on single or multiple operands like addition, substraction etc. In the below example, + is the operator.

<?php
$x = 10;
$y = 20;
$sum = $x + $y;
echo($sum);
?>

Check result here

2. Operand

An operand is what operators are applied on. In the above example x and y are the operands.

Types of Operators in PHP

1. Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on operands.

OperatorDescriptionExample
+Used to perform Addition8+2 = 10
-Used to perform Subtraction12-2 = 10
*Used to perform Multiplication5*2 = 10
/Used to perform Division100/10 = 10
%Used to return Remainder40%10 = 0
++Used to perform Incrementa=10;a=10; a++; // a becomes 11
--Used to perform Decrementa=10;a=10; a--; // a becomes 9

Example

<?php
	$x = 90;
  $y = 10;

  $sum = $x + $y;
  echo("Sum of two numbers: $sum\n");
  $diff = $x - $y;
  echo("Difference between two numbers: $diff\n");
  $multiply = $x * $y;
  echo("Product of two numbers: $multiply\n");
  $div = $x / $y;
  echo("Division of two numbers: $div\n");
  $mod = $x % $y;
  echo("Modulus of two numbers: $mod\n");
  $x++;
  echo("x value after incrementing :$x\n");
  $x--;
  echo("x value after decrementing :$x\n");
?>

Check Result here

2. Comparison Operators

Comparison operators are used to compare two operands.

OperatorDescriptionUsage
==Is equal tox == y
!=Not equal to!=x
>Greater thanx > y
>=Greater than or equal tox >= y
<Less thanx < y
<=Less than or equal tox <= y

Example

<?php
  $x = 90;
  $y = 10;

  if ( $x == $y) {
  echo("$x and $y are equal\n");
  }
  if ( $x != $y) {
  echo("$x and $y are not equal\n");
  }
  if ( $x > $y) {
  echo("$x is greater than $y\n");
  }
  if ( $x < $y) {
  echo("$x is less than $y\n");
  }
?>

Check Result here

3. Logical operators

Below are the logical operators present in the Php.

OperatorDescriptionUsage
&&Logical AND(x > y) && (y > z)
||Logical OR(x > y) || (y > z)
!Logical NOT(!x)
andLogical AND(x and y)
orLogical OR

4. Assignment Operators

Below are the assignment operators present in the Php.

OperatorDescriptionUsage
=Assignint x = 10;
+=Add and assignint x=10; x+=30; // x becomes 40
-=Subtract and assignint x=40; x-=10; // x becomes 30
*=Multiply and assignint x=10; x*=40; // x becomes 400
/=Divide and assignint x=100; x /= 10;// x becomes 10
%=Modulus and assignint x=100; x%=10; // x becomes 0

Example

<?php

  $x = 10; // assigning 10 to $x 
  echo("x value: $x\n");
  
  $x+=30;
  echo("x value after += operation:  $x\n");
  
  $x-=10;
  echo("x value after -= operation:  $x\n");
  
  $x*=10;
  echo("x value after *= operation:  $x\n");
  
  $x/=10;
  echo("x value after /= operation:  $x\n");
  
  $x%=10;
  echo("x value after %= operation:  $x\n");
?>

Check Result here

5. Conditional Operator

If the operator is applied on three operands then it is called conditional operator. This is also known as ternary operator as a condition is followed by ? and true-expression which is followed by a : and false expression. This is oftenly used as a shortcut to replace if-else statement.

Example

<?php
  $x = 10;
  $y = 50;
  $largerNumber = ($x > $y) ? $x : $y;
  echo("Larger number is :$largerNumber");
?>

Check Result here

Summary

Operator typeDescription
Arithmetic Operator+ , - , * , / , %, ++, --
comparision Operator< , > , <= , >=, != , ==
Logical Operator&& , ||, !
Assignment Operator= , += , -= , *= , /= , %=
Conditional Operator? :