Operators are used to perform some operations on data operators are broadly classified into 2 categories
- Unary operators
An operator that requires only one operant in unary operator.
Ex: $a++
in the above statement a is operant and ++ is operator - Binary Operator
An operator that require two operant is called as binary operator.
Ex: $a+$b
We have 8 types of operators in PHP those are
- Arithmetic operators
- +
- –
- *
- /
- %
<?php echo(10/3)."<br>"; echo(10%3)."<br>"; echo(-10%3)."<br>"; echo(10%-3)."<br>"; echo(-10%-3)."<br>"; $a=5; $b="psd"; $c=$a+$b; echo $c; ?> the output is 3.3333333333333 1 -1 1 -1 5
- Assignment operators
- =
- +=
- -=
- *=
- /=
- %=
- .=
<?php $a=($b=4)+5; echo $a." <?php $a=($b=4)+5; echo$a; echo$b; ?> The output is
9
4 - increment and decrement Operators
- ++(increases value by one)
- –(Decrease value by one)
<?php $x=10; echo $x++."--".++$x; ?> Output is 10--12
- Relation operators
- <
- >
- <=
- >=
- == (will check only for content is same or not)
- !=
- <>
- === (will check content and type of content also)
- !==
- Logical operators (it was used to frame a compound condition)
- &&
- ||
In case of logical operators all the expressions will not be evaluated every time . It is based on the result of first condition.(($a>$b)&&($a>$c))
- Negation operators
it used to multiply a number with “-“ operator$a=10;
$b=-$a;
- Error Control operators (@)
when prepended with an expression, any warning messages that is being generated will gets suppressed.
- Bitwise operators
Bit wise operators are 3 they are
- AND –&
- OR — |
- XOR — ^
In this case bit wise operators both operant will converted to binary form and corresponding bitwise operation will be performed.
- Shift Operators
- Left Shift operator (<<)
- Right shift operator (>>)
In this case of shift operators the first operant will be converted to binary form and corresponding shifts will happen based on second number of operant.EX:–
echo (100>>2);
echo (100<<2);
output will be
25
400
- String / Concadination operator
- .
- .=
- concadination / ternary operator
- ?:
- Type operator
- “insaneof”
- New Operator
- new
- Scope operator
- ::
- De-referencing operator
- ->