|
An Operator is a one type of symbol
that is used to perform certain operation on its operands.
Operators
are always associated with Operands.
Example
a + b
Here,
a and b are operands while + is operator. |
Types of Operator
|
|
Various Operators available in PHP are:
(1) Arithmatic Operator
(2) Assignment Operator
(3) Comparision Operator
(4) Logical Operator
(5) Increment and Decrement Operator
(6) Ternary Operator
(7) Bitwise Operator
(8) String Concate Operator |
Arithmatic Operator
|
|
Arithmatic
Operators are used to perform mathematical operations on its operands.
|
Operator |
Purpose |
Example |
|
+ |
Perform addition of its two operands. |
$x=5;
$y=3;
$z=$x + $y;
$z=8 |
|
- |
Perform substraction of its two operands. |
$x=5;
$y=3;
$z=$x - $y;
$z=2 |
|
* |
Perform multiplication of its two operands. |
$x=5;
$y=3;
$z=$x * $y;
$z=15 |
|
/ |
Divide left operand by right operand. |
$x=5;
$y=2;
$z=$x / $y;
$z=2.5 |
|
% |
Divide left operand by right operand and return remainder of division. |
$x=5;
$y=3;
$z=$x % $y;
$z=2 |
|
Assignment Operator
|
It allows you to assign value of one variable or expression
to another variable.
|
Operator |
Purpose |
Example |
|
= |
Assign value of its right operand to its left operand. |
$a = 5;
$b = $a;
Thus $b = 5 |
|
+= |
Add value of its right operand to the value of its left
operand and assign the result to its left operand. |
$a = 5;
$a += 5;
$a=10; |
|
-= |
Subtract value of its right operand from the value of
its left operand and assign the result to its left operand. |
$a = 5;
$a -= 2;
Thus $a = 3 |
|
*= |
Multiply value of its right operand with the value of
its left operand and assign the result to its left operand. |
$a = 5;
$a *= 5;
Thus $a = 25 |
|
/= |
Divide value of its left operand by the value of its right
operand and assign the result to its left operand. |
$a = 4;
$a /= 2;
Thus $a = 2 |
|
%= |
Divide value of its left operand by the value of its right
operand and assign remainder of division to its left operand. |
$a = 5;
$a %= 2;
Thus $a = 1 |
|
Comparision Operator
|
It allows you to perform comparison between two operands. It is widely used in testing conditions.
|
Operator |
Purpose |
Example |
|
== |
Determines weather the value of both operands are equal or not. If value of both operands are equal then it returns true otherwise false. |
$a = 5;
$b = 8;
$a == $b
It returns false. |
|
!= |
Determines weather the value of both operands are equal or not. If value of both operands are not equal then it returns true otherwise false. |
$a = 5;
$b = 8;
$a != $b
It returns true. |
|
> |
Determines weather value of its left operand is greater then the value of its right operand or not. If value of left operand is greater then the value of its right operand then it returns true otherwise false. |
$a = 5;
$b = 8;
$a > $b
It returns false. |
|
< |
Determines weather value of its left operand is less then the value of its right operand or not. If value of left operand is less then the value of its right operand then it returns true otherwise false. |
$a = 5;
$b = 8;
$a < $b
It returns true. |
|
>= |
Determines weather value of its left operand is greater then or equal to the value of its right operand or not. If value of left operand is greater then or equal to the value of its right operand then it returns true otherwise false. |
$a = 5;
$b = 8;
$a >= $b
It returns false. |
|
<= |
Determines weather value of its left operand is less then or equal to the value of its right operand or not. If value of left operand is less then or equal to the value of its right operand then it returns true otherwise false. |
$a = 5;
$b = 8;
$a <= $b
It returns true. |
|
=== |
Determines weather both operands are equal in terms of value as well as data type or not. If both operands are equal then it returns true otherwise false. |
$a = 5;
$b = 8;
$a === $b
It returns false. |
|
Logical Operator
|
It allows you to perform logical operation on its operands.
Logical
Operators are used to combine more then one condition.
If
you want to check more then one condition at a same time and based on the result
of that conditions want to perform some action then you can use Logical Operator.
|
Operator |
Purpose |
Example |
|
&& |
Performs logical AND operation on its operand. If both operands are true then it returns true otherwise it returns false. |
$a = true;
$b = false;
$c = $a && $b;
Thus
$c = false |
|
|| |
Performs logical OR operation on its operand. If both
operands are false then it returns false otherwise it returns true. |
$a = true;
$b = false;
$c = $a || $b;
Thus $c = true |
|
! |
Performs logical NOT operation on its single operand. If its operand is true then it returns false and if its operand is false then it returns true. |
$a = true;
$c = ! $a;
Thus $c = false |
|
Increment & Decrement Operator
|
|
Incremnt Operator is used to increment the value of its operand by one.
Decrement Operator is used to decrement the value of its operand by one.
|
Operator |
Purpose |
Example |
|
++ |
Increment value of its operand by one. |
$a=5;
$a++;
Now $a=6 |
|
-- |
Decrement value of its operand by one. |
$a=5;
$a--;
Now $a=4 |
|
Ternary Operator
|
Ternary
Operator is an alternative of If ... else statement.
Ternary
Operator having three opernads thats why it is also known as Ternary Operator.
General
Syntax of Ternary Operator is
(Condition) ? Statement1: Statement2;
|
Operator |
Purpose |
Example |
|
?: |
It will first test for the condition.
If condition evaluates to true then it will execute statement after ?, otherwise it will execute statement after : |
$a=10;
$b=20;
$max = ($a > $b)? $a: $b;
Thus
$max = 20 |
|
Bitwise Operator
|
It
allows you to manipulate operands at bit level.
Using
Bitwise Operator we can work with individual bits of integer number.
|
Operator |
Purpose |
Example |
|
& |
Performs bitwise and operation on its two operands. |
$a = 5;
$b = 5;
$c = $a & $b;
Thus
$c = 5 |
|
| |
Performs bitwise or operation on its two operands. |
$a = 5;
$b = 14;
$c = $a | $b;
Thus
$c = 15 |
|
^ |
Performs bitwise xor operation on its two operands. |
$a = 5;
$b = 14;
$c = $a ^ $b;
Thus
$c = 11 |
|
~ |
Performs bitwise not operation on its single operand. |
$a = 5;
$c = ~ $a;
Thus
$c = -6 |
|
<< |
Performs bitwise shift left operation by Shifting all bits in the left operand by number of places specified by right operand. |
$a = 5;
$c = $a << 2;
Thus
$c = 20 |
|
>> |
Performs bitwise shift right operation by Shifting all bits in the left operand by number of places specified by right operand. |
$a = 5;
$c = $a >> 2;
Thus $c = 1
|
|
String Concate Operator
|
It allows you to Concate two or more strings operands.
|
Operator |
Purpose |
Example |
|
. |
Concates its string operands. |
$a="HE";
$b="LLO";
$c=$a.$b;
$c="HELLO" |
|
| |