php Operators:
Operators are one of the important feature of every language. In php operators are classified in following categories:
- ArithmeticOperator
- Comparison Operator
- Assignment Operator
- Logical Operator
- String Operator
- Bitwise Operator
- Error Control Operator
- Execution Operators
- Array Operators
- Type Operators
Name | Symbol | Example | Output |
Addition | + | 2+2 | 4 |
Subtraction | - | 2-2 | 0 |
Division | / | 2/2 | 1 |
Multiplication | * | 2*2 | 4 |
Modulus | % | 2%2 | 0 |
Increment | ++ | let $x=2 ++$x |
3 |
Decrement | -- | let $y=5 --$y |
4 |
Negation | - | let $a=2 -$a |
-2 |
Name | Symbol | Example | Output |
is equal | = = | 2= =2 | True |
is not equal | != | 2!=1 | True |
is not equal | <> | 2!=2 | False |
is greater than | > | 2>1 | True |
is less than | < | 3<4 | True |
is less than equal | <= | 2<=4 | True |
is greater than equal | >= | 2>=4 | False |
Name | Symbol | Example | Output/Meaning |
assignment | = | $x=2 | assigns 2 into x |
combined operator (addition) | += | let $x=2 $x+=2 |
value of x will be 4 |
combined operator (subtraction) | -= | let $x=2 $x-=2 |
value of x will be 0 |
combined operator (multiplication) | *= | let $x=3 $x*=2 |
value of x will be 6 |
combined operator (division) | /= | let $x=2 $x/=2 |
value of x will be 1 |
combined operator (modulus) | %= | let $x=2 $x%=2 |
value of x will be 0 |
Name | Symbol | Example | Output/Meaning |
And | and | $x and $y | true if both are true |
Or | or | $x or $y | true if at least one is true |
Xor | xor | $x xor $y | true if both equal, false if both unequal |
Not | ! | ! $a | returns true if variable is false, and vice versa |
And | && | $a && $b | true if both are true |
Or | || | $a || $b | true if at least one variable is true |
Name | Symbol | Example | Output/Meaning |
Concatenation Operator | . (Period symbol) | let $a="Hello", $b="World" $a.$b |
HelloWorld |
Concatenating Assignment Operator | .= | let $a="Hello", $a.="World" |
HelloWorld |
Name | Symbol | Example | Output/Meaning |
And | & | let $a=11 and $b=5 $a & $b |
1 |
Or (Inclusive Or) | | | let $a=11 and $b=5 $a | $b |
15 |
Xor (Exclusive Or) | ^ | let $a=11 and $b=5 $a ^ $b |
14 |
Not | ~ | let $a=11 ~ $a |
-12 |
Right shift | >> | let $a=11 and $b=2 $a >> $b |
2 |
Left shift | << | let $a=11 and $b=2 $a << $b |
44 |
Name | Symbol | Example | Output/Meaning |
Error control operator | @ | $var=@$array['name']; | will not generate any error message if index 'name' is not present |
Name | Symbol | Example | Output/Meaning |
backticks | `` | `ls -l` | this operator is used to execute shell commands, the output can be stored in a variable also. |
Let us consider the following arrays:
$a=array("lang"=>"Java", "platform"=>"Cross");
$b=array("lang"=>"php", "platform"=>"Cross", "Year"=>"1995");
Name | Symbol | Example | Output/Meaning |
Union | + | $a + $b | Array ( [lang] => php [paradigm] => imperative [Year] => 1995 ) |
Equality | = = | $a = = $b | (Since both are unequal, no output will be generated) |
Identity | = = = | $a = = = $b | (Since both are unequal, no output will be generated) |
Inequality | != | $a != $b | 1 (Since both are unequal) |
Inequality | <> | $a <> $b | 1 (Since both are unequal) |
Non-identity | != = | $a != = $b | 1 (Since both are unequal) |
In this example let us consider the following case:
<?php
class First{}
class Second{}
$a=new First();
print_r($a instanceof First);
print_r($a instanceof Second);
?>
Name | Symbol | Example | Output/Meaning |
instanceof | instanceof | AS ABOVE | 1 (Since $a is the instance of class First) and On the second line nothing will be generated as $a is not the instance of class Second |