Operators in PHP

In PHP there is a list of operators which can be used in scripting. They are as follows:

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

Assignment Operators:

In PHP assignment operators are used to assign the value to variables.

Operator Description Example
     = Assigning the value $a = 10
   += Addition $a += $b or $a =$a + $b
   -= Subtraction $a -= $b or $a = $a – $b
   *= Multiplication $a *= $b or $a = $a * $b
   /= Division $a /= $b or $a = $a / $b
   %= Modula $a %= $b or $a = $a % $b

Example:

With following example you can understand how to use these assignment operators.

Arithmetic Operators:

In PHP arithmetic operators are used to do mathematical operation on two or more variables.

Operator Description Example
     + Addition $a + $b
   – Subtraction $a – $b
   * Multiplication $a * $b
   / Division $a / $b
   % Modula $a % $b
   ** Exponentiation $a ** $b

Example:

With following example you can understand how to use these arithmetic operators.

 

String Operators:

In PHP string operators are used to concatenate the value of two or more variables. In PHP there are only two string operators.

Operator Description Example
     . Concatenate $a . $b
   .= Concatenate with assignment $a .= $b

Example:

With following example you can understand how to use these string operators.

Increment & Decrement Operators:

In PHP Increment operators are used to increment the value of the variables and the Decrement operators are used to decrement the value of the variables.

Operator Description Example Assume $a=10 Than O/P
     ++$a Pre-Increment 11
   $a++ Post-Increment 10
   –$a Pre-Decrement 9
   $a– Post-Decrement 10

Example:

With following example you can understand how to use these increment and decrement operators.

Comparison Operators:

In PHP comparison operators are used to compare the values of two variables (numbers or strings).

Operator Description Example
     == Is equal $a==$b
   === Is identical equal $a===$b
   != Is not equal $a!=$b
   <> Is not equal $a<>$b
   !== Is not identical $a!==$b
   > Is greater than $a>$b
   < Is less than $a<$b
   >= Is greater than or equal $a>=$b
   <= Is less than or equal $a<=$b

Example:

With following example you can understand how to use these comparison operators.

Logical Operators:

In PHP logical operators are used when you are working on conditional statement.

Operator Description Example
     and And (if both are true) $a and $b
   or Or(if either $a or $b is true) $a or $b
   xor Xor(if either $a or $b is true but not both ) $a xor $b
   && And(True if both are true) $a && $b
   || Or(if either $a or $b is true) $a || $b
   ! Not(if $a is not true) !$a

Example:

With following example you can understand how to use these logical operators.

Array Operators:

In PHP array operators are used to compare arrays.

Operator Description Example
     + Union $a + $b
   == Equality $a == $b
   === Identity $a === $b
   != Inequality $a != $b
   <> Inequality $a <> $b
 !== Non- Identity $a !== $y

Example:

With following example you can understand how to use these logical operators.

Leave a Reply

Your email address will not be published. Required fields are marked *