Please check the following Operators with $a=10 , $b=4
Operator | Example | Result | Name | Description |
---|---|---|---|---|
+ | $a + $b | 14 | Addition | Sum of $a and $b |
- | $a - $b | 4 | Subtraction | Difference of $a and $b |
* | $a * $b | 40 | Multiplication | Product of $a and $b |
/ | $a / $b | 2.5 | Division | Quotient of $a and $b |
% | $a % $b | 2 | Modulo | Remainder of $a divided by $b |
** | $a ** $b | 10000 | Exponentiation | Result of raising $a to the $b'th power |
+ | +$a | 10 | Identity | Conversion of $a to int or float |
- | -$a | -10 | Negation | Opposite of $a |
The PHP assignment operator is "+". In assignment operator value is assigned like $a=10 , $b=4.
Example | Same As | Result | Description |
---|---|---|---|
$a = $b | $a = $b | 4 | The left operand get the value of right operand |
$a += $b | $a = $a + $b | 14 | Addition |
$a -= $b | $a = $a - $b | 6 | Subtraction |
$a *= $b | $a = $a * $b | 40 | Multiplication |
$a /= $b | $a = $a / $b | 2.5 | Division |
$a %= $b | $a = $a % $b | 2 | Modulus |
Result with $a=10 , $b=4.
Operator | Example | Result | Name | Description |
---|---|---|---|---|
& | $a & $b | 0 | And | Bits that are set in both $a and $b are set |
| | $a | $b | 14 | Or | Bits that are set in either $a or $b are set. |
^ | a ^ b | 14 | Xor | Bits that are set in $a or $b but not both are set |
~ | ~ $a | -11 | Not | Bits that are set in $a are not set, and vice versa |
<< | $a << $b | 160 | Shift left | Shift the bits of $a $b steps to the left |
<< | $a >> $b | 0 | Shift right | Shift the bits of $a $b steps to the right |
Operator | Example | Name | Description |
---|---|---|---|
== | X == Y | Equal | TRUE if X is equal to Y |
=== | X === Y | Identical | TRUE if X is equal to Y, and they are of the same data type(int,string,float,array,object etc) |
!= | X != Y | Not equal | TRUE if X is not equal to Y after type juggling. |
<> | X <> Y | Not equal | TRUE if X is not equal to Y after type juggling. |
!== | X !== Y | Not identical | TRUE if X is not equal to Y, or they are not of the same data type(int,string,float,array,object etc). |
< | X < Y | Less than | TRUE if X is strictly less than Y. |
> | X > Y | Greater than | TRUE if X is strictly greater than Y. |
<= | X <= Y | Less than or equal to | TRUE if X is less than or equal to Y. |
>= | X >= Y | Greater than or equal to | TRUE if X is greater than or equal to Y. |
<=> | X <=> Y | Spaceship | TRUE An integer less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively. |
The sign of error control operator is (@), it's used to hide the error messages that might be generated by that expression
echo $string; Output:: it shows Notice:Undefined variable
echo @$string; Output::It does not show any such type of "Notice"
The sign of execution operator is backticks (``), it's used to execute as a shell command
$website = 'www.example.com'; echo `ping -n 5 {$website}`;
echo @$string; Output::It does not show any type of "Notice"
PHP Incrementing/Decrementing operators are used to increment/decrement a variable's value.
The increment/decrement operators only worked on numbers and strings. Arrays, objects, booleans and resources are not affected by Incrementing/Decrementing operators.
If variable's value is NULL then decrement has no effect,in case of increment value is 1
$k=NULL; $k++; echo $k;
//Output :: 1
$k=NULL; --$k; echo $k;
//Output ::
Result with $a=5; $b=10; $c=20; $d=25;.
Operator | Result | Name | Description |
---|---|---|---|
++$a | 6 | Pre-increment | Increments $a by one, then returns $a. |
$b++ | 10 | Post-increment | Returns $b, then increments $a by one. |
--$c | 19 | Pre-decrement | Decrements $c by one, then returns $c. |
$d-- | 25 | Post-decrement | Returns $d, then decrements $d by one. |
Operator | Example | Name | Description |
---|---|---|---|
and | $a and $b | And | TRUE if both $a and $b are TRUE |
or | $a or $b | Or | TRUE if either $a or $b is TRUE |
xor | $a xor $b | Xor | TRUE if either $a or $b is TRUE, but not both. |
! | ! $a | Not | TRUE if $a is not TRUE |
&& | $a && $b | And | TRUE if both $a and $b are TRUE |
|| | $a || $b | Or | TRUE if either $a or $b is TRUE |
PHP has two String operators only, first is the concatenation operator ('.') and second is the concatenating assignment operator ('.=')
$string1 = "testing"; $string2 = "I am ".$string1; echo $string2; //Output :: I am testing
$string1 = "I am"; $string1 .= " testing"; echo $string1; //Output :: I am testing
Operator | Example | Name | Description |
---|---|---|---|
+ | $a + $b | Union | Union of $a and $b |
== | $a == $b | Equality | TRUE if $a and $b have the same key/value pairs. |
=== | $a === $b | Identity | TRUE if $a and $b have the same key/value pairs in the same order and of the same types. |
!= | $a != $b | Inequality | TRUE if $a is not equal to $b |
<> | $a <> $b | Inequality | TRUE if $a is not equal to $b |
!== | $a |== $b | Non-identity | TRUE if $a is not identical to $b |
The + operators return the combined array of left-hand and right-hand if duplicate keys are found , left-hand array will be used and the right-hand array will be ignored
$a = array("a" => "PHP", "c" => "JS"); $b = array("a" => "HTML", "b" => "CSS", "d" => "Python"); $c = $a + $b; // Union of $a and $b var_dump($c); //Output :: array (size=4) 'a' => string 'PHP' (length=3) 'c' => string 'JS' (length=2) 'b' => string 'CSS' (length=3) 'd' => string 'Python' (length=6)
The instanceof operator is used to check the variable is an instantiated object of a class or not, if instance is created then it will return TRUE
class classA { } class classB { } $a = new classA; var_dump($a instanceof classA); // true var_dump($a instanceof classB); // false
class classA { } class classB extends classA { } $a = new classB; var_dump($a instanceof classA); // true var_dump($a instanceof classB); // true
interface interfaceA { } class classB implements interfaceA { } $a = new classB; var_dump($a instanceof classB); // true var_dump($a instanceof interfaceA); // true
interface interfaceA { } class classB implements interfaceA { } $a = new classB; $b = new classB; $c = 'classB'; $d = 'NotClass'; var_dump($a instanceof $a); // true var_dump($a instanceof $b); // true var_dump($a instanceof $c); // true var_dump($a instanceof $d); // false