PHP Operators

PHP Operators


Operators are symbols that tell the PHP processor to perform certain actions. The expression 5 – 1 is equal to 4. Here 5 and 1 are called operands and – is called the operator. Operators are used to perform operations on variables and values.
 
PHP language supports the following type of operators.
  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Increment/Decrement operators
  5. Logical operators
  6. String operators
  7. Array operators
 
1. Arithmetic operators: The arithmetic operators in PHP perform general arithmetical operations, such as addition, subtraction, multiplication and division etc.
Name
Operator
What does it do?
Example
Addition
+
It is used to perform normal addition.
$a + $b
Subtraction
-
It is used to perform normal subtraction.
$a - $b
Multiplication
*
It is used to perform multiplication.
$a * $b
Division
/
It is used to perform division.
$a / $b
Exponent
**
It returns the first operand raised to the power the second operand. $a ** $b = $a$b
$a ** $b
Modulus(or, Remainder)
%
It returns the remainder of first operand divided by the second operand
$a % $b
 
2. Assignment Operators: Assignment operators are performed with numeric values to store a value to a variable. The default assignment operator is “ = ”. This operator sets the left side operant value of expression to right side variable.
Operator
Usage
=
$a = $b, will save the value of variable $b to the variable $a
+-
$a += $b is same as $a + $b
-=
$a -= $b is same as $a - $b
*=
$a *= $b is same as $a * $b
/=
$a /= $b is same as $a / $b
%=
$a %= $b is same as $a % $b
 
3. Comparison Operators: Comparison operators perform an action to compare two values. These values may contain integer or string data types (Number or Strings).
Name
Operator
What does it do?
Example
Equal
==
It returns true if left operand is equal to the right operand.
$a == $b
Identical
===
It returns true if left operand is equal to the right operand and they are of the same type.
$a === $b
Not Equal
!=
It returns true if left operand is not equal to the right operand.
$a != $b
Not Identical
!==
It returns true if left operand is not equal to the right operand, and they are of different type as well.
$a !== $b
Greater than
>
It returns true if left operand is greater than the right operand.
$a > $b
Less than
<
It returns true if left operand is less than the right operand.
$a < $b
Greater than or equal to
>=
It returns true if left operand is greater than or equal to the right operand.
$a >= $b
Less than or equal to
<=
It returns true if left operand is less than or equal to the right operand.
$a <= $b
 
4. Increment and Decrement Operators: Increment and decrement operators are used to perform the task of increasing or decreasing variable’s value. This operator is mostly used during iterations in the program logics.
Operator
Usage
++$a
Pre Increment, It will first increment the operand by 1(add one to it) and then use it or return it.
$a++
Post Increment, It will first return the operand and then increment the operand by 1.
--$b
Pre Decrement, It will first decrement the operand by 1(subtract one from it) and then use it or return it.
$b--
Post Decrement, It will first return the operand and then decrement the operand by 1.
 
5.Logical Operators:The PHP logical operators are used with conditional statements and expressions. So basically conditional operators are used to combine conditional statements.
Operator
 Name
 Example
  Result
and
And
$a and $b
   True if both $a and $b are true
or
Or
$a or $b
  True if either $a or $b is true
xor
Xor
 $a xor $b
   True if either $a or $b is true, but not both
&&
And
 $a && $b
   True if both $a and $b are true
||
Or
 $a || $b
   True if either $$a or $b is true
!
Not
 !$a
   True if $a is not true
 
6. String Operators: Two operators are used to perform string related operations such as Concatenation and Concatenation assignment (Appends).
Name
Operator
What does it do?
Example
Concatenation
. (a dot)
It is used to concatenate(join together) two strings.
$a.$b
Concatenation Assignment
.=
It is used to append one string to another.
$a .= $b
 
7.Array Operators :The PHP array operators are used to compare arrays.
Operator
Name
Example
Result
+
Union
$a + $b
Union of $a and $b
==
Equality
$a == $b
True if $a and $b have the same key/value pairs
===
Identity
$a === $b
True if $a and $b have the same key/value pairs in the same order and of the same types
!=
Inequality
$a != $b
True if $a is not equal to $b
<> 
Inequality
$a <> $b
True if $a is not equal to $b
!==
Non-identity
$a !== $b
True if $a is not identical to $b