PHP Operator

PHP Operator are used to perform operations such as addition, division, comparison, etc. with variables. When you use operator with a variable then that variable is called operand.

PHP divides the operators in the following groups:

  • Arithmetic Operators
  • Assignment Operators
  • Logical Operators
  • Comparison Operators
  • Increment/Decrement Operators
  • String Operators
  • Array Operators

Arithmetic Operators

Arithmetic php operator are used to perform arithmetic operations with variables. A list of some common arithmetic operators is being given below.

OperatorNameExampleExplanation
     +Addition$a + $bAddition of 2 variables
     –Subtraction$a – $bSubtraction of 2 variables
     *Multiplication$a * $bMultiplication of 2 variables
    /Division$a / $bDivision of 2 variables
    %Modulus$a % $bRemain from division
    **Exponentiation$a ** $bFirst variable raise to power to second variable

Assignment Operators

Assignment php operator is used to assign a variable to a value. (=) assignment operator. These values ​​can be assigned by 2 types.

  • In the first method, you assign the value directly. Assigning Direct Value is quite simple. You create a variable and assign (=) the assignment operator and write the value in front of it.
$name = "Ram";
  • In the second way you assign a variable to another variable. By doing so, the value of the variable of the right side gets assigned to the variable of the left side. This is called assigning by reference.
$a = 9;
$b = $a   //$b contains 9
AssignmentExplanation
$a = $bThe value of the variable of the right side gets assigned to the variable of the left side
$a += $bAddition
$a -= $bSubtraction
$a *= $bMultiplication
$a /= $bDivision
$a %= $bModulus

Logical Operators

The logical php operator are used to combine conditional statements.

OperatorNameExampleExplanation
andAnd$a and $bTrue if both $a and $b are true
orOr$a or $bTrue if either $a or $b is true
xorXor$a xor $bTrue if either $a or $b is true, but not both
&&And$a && $bTrue if both $a and $b are true
||Or$a || $bTrue if either $a or $b is true
!Not!$aTrue if $a is not true

Comparison Operators

Comparison operators are used to compare values ​​of two variables. Comparison php operator are used mostly in control statements. The result of Comparison variables is TRUE or FALSE.

OperatorNameExampleExplanation
==Equal$a == $bTrue if $a is equal to $b
===Identical$a===$bTrue if $a is equal to $b, and they are of the same type
!=Not equal$a != $bTrue if $a is not equal to $b
<>Not equal$a <> $bTrue if $a is not equal to $b
!==Not identical$a !==$bTrue if $a is not equal to $b, or they are not of the same type
>Greater than$a > $bTrue if $a is less than $b
<Less than$a < $bTrue if $a is greater than $b
>=Greater than or equal to$a >= $bTrue if $a is greater than or equal to $b
<=Less than or equal to$a <= $bTrue if $a is less than or equal to $b

Increment and Decrements PHP Operator

With increment and decrements operators, you can increment and decrement the value of a variable. These operators (++) and (–) are. Such variables can be used in 2 ways.

  • Post increment / decrements – In this kind of use, operators are placed behind the variable. After printing the variable, it increases or decreases its value from number 1.
$a++;
$a--;
  • Pre increment / decrements – In this kind of use, php operator are placed before the variable. These increments increase or decrease before printing.
++$a;
--$a;
OperatorNameExplanation
$a++Post-incrementReturns $a, then increments $a by one
++$aPre-incrementIncrements $a by one, then returns $a
$a–Post-decrementReturns $a, then decrements $a by one
–$aPre-decrementDecrements $a by one, then returns $a

String Operator

There are two operators for strings.

OperatorNameExampleExplanation
     .Concatenation$a . $bConcatenation of $a and $b
    .=Concatenation assignment$a .= $bAppends $a to $b

Array Operators

PHP also provides some array operators that only apply to arrays.

OperatorNameExampleExplanation
     +Union$a + $bUnion of $a and $b
    ==Equality$a == $bReturns true if $a and $b have the same key/value pairs
    ===Identity$a===$bReturns true if $a and $b have the same key/value pairs in the same order and of the same types
    !=Inequality$a != $bReturns true if $a is not equal to $b
    <>Inequality$a <> $bReturns true if $a is not equal to $b
    !==Non-identity$a !== $bReturns true if $a is not identical to $b