Following are the different type of operators in PHP.
Operator Precedence: When we use multiple operator in one statement, operator run according to the priority. Following are the table of operator precedence which we must keep in mind while doing any calculation in PHP OR running multiple operator in one statement. we can also change the operator priority by using the bracket "()".
Arithmetic Operators:These are those operator which we mostly use in calculation like plus, minus, divide, multiple, concat and change operator, modulus (+,-,/, x, .,-,%). In PHP to multiple two number or more number we use "*" instead of "x";
Assignment Operators: Assignment operator are used to assign the values to a variable.for example $a=10; Here 10 are assign to a variable (i.e $a). In Following example, Example1 and Example2 works same as the doing method is different.
Bitwise Operators:Bitwise operator are used to do the manipulate the according to their operators. Bitwise operator work on number.
Comparison Operators: Comparison Operator are used to compare two variable having some values. when we are comparing two values they mostly return true or false. For Example 10<15 will return true as 10 is less then 15. Following table describe more.
Error Control Operators: "@" is known as error control. As if we use this operator then error will not be generator for that statement. See is example below.
when we run able code, we are expecting the "$aa" should be generate error, but is not because we have use @ operator in front of $aa.
Increment / Decrement Operator: When we use this operator, variable value is increased in increment and value is decreased in
Logical Operator: Logical Operator are used to compare the two string/number.
String Operators: The operator which work on only on string. For Example "." and ".=" is known as string operator. 1st used to concat two or more strings. 2nd is work same but return the values.
for more detail read above.
Array Operators: The Operator which work for array known as array operator. Array operator are very useful while you are developing any website. Most of developer are unware of this. instead of using these function they created a function which work like this.
Means If you know about these operator, it will save your time and also minimize the code.
- Operator Precedence
- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Comparison Operators
- Error Control Operators
- Execution Operators
- Incrementing/Decrementing Operators
- Logical Operators
- String Operators
- Array Operators
- Type Operators
Operator Precedence: When we use multiple operator in one statement, operator run according to the priority. Following are the table of operator precedence which we must keep in mind while doing any calculation in PHP OR running multiple operator in one statement. we can also change the operator priority by using the bracket "()".
Operator Precedence
Associativity | Operators | Additional Information |
non-associative | clone new | clone and new |
left | [ | array() |
right | ++ -- ~ (int) (float) (string) (array) (object) (bool) @ | types and increment/decrement |
non-associative | instanceof | types |
right | ! | logical operator |
left | * / % | arithmetic operator |
left | + - . | arithmetic and string |
left | << >> | bitwise operator |
non-associative | < <= > >= | comparison operator |
non-associative | == != === !== <> | comparison operator |
left | & | bitwise and references operator |
left | ^ | bitwise operator |
left | | | bitwise operator |
left | && | logical operator |
left | || | logical operator |
left | ? : | ternary operator |
right | = += -= *= /= .= %= &= |= ^= <<= >>= => | assignment operator |
left | and | logical operator |
left | xor | logical operator |
left | or | logical operator |
left | , | many uses |
Arithmetic Operators:These are those operator which we mostly use in calculation like plus, minus, divide, multiple, concat and change operator, modulus (+,-,/, x, .,-,%). In PHP to multiple two number or more number we use "*" instead of "x";
Operators | Name of operators | Detail | Example | Output |
a + b | Addition | Sum of a and b | 2 + 2 | 4 |
a - b | Subtraction | Difference of a and b | 5 - 2 | 3 |
a * b | Multiply | Product of a and b | 5 * 2 | 10 |
a / b | Divide | Quotient of a and b | 15 / 5 | 3 |
a % b | Modulus | Remainder of a divided by b | 5 % 2 10 % 8 10 % 2 |
1 2 0 |
- a | Oppisite | Opposite of a | - 2 | |
a . b | Concatenation | Concatenate two strings | "php" . "certification" | php certification |
Assignment Operators: Assignment operator are used to assign the values to a variable.for example $a=10; Here 10 are assign to a variable (i.e $a). In Following example, Example1 and Example2 works same as the doing method is different.
Example1 | Example2 | Operator |
x = y | x = y | The left operand gets set to the value of the expression on the right |
x += y | x = x + y | Addition |
x -= y | x = x - y | Subtraction |
x *= y | x = x * y | Multiplication |
x /= y | x = x / y | Division |
x %= y | x = x % y | Modulus |
a .= b | a = a . b | Concatenate two strings |
Bitwise Operators:Bitwise operator are used to do the manipulate the according to their operators. Bitwise operator work on number.
Example | Name | Result |
$a & $b | And | Bits that are set in both $a and $b are set. |
$a | $b | Or (inclusive or) | Bits that are set in either $a or $b are set. |
$a ^ $b | Xor (exclusive or) | Bits that are set in $a or $b but not both are set. |
~ $a | Not | Bits that are set in $a are not set, and vice versa. |
$a << $b | Shift left | Shift the bits of $a $b steps to the left (each step means "multiply by two") |
$a >> $b | Shift right | Shift the bits of $a $b steps to the right (each step means "divide by two") |
Comparison Operators: Comparison Operator are used to compare two variable having some values. when we are comparing two values they mostly return true or false. For Example 10<15 will return true as 10 is less then 15. Following table describe more.
Operator | Name of Operator | Example |
a == b | Equal | 5==8 returns false |
a === b | Identical | 5==="5" returns false |
a != b | Not equal | 5!=8 returns true |
a<>b | Not equal | 5<>8 returns true |
a !== b | Not identical | 5!=="5" returns true |
a>b | Greater than | 5>8 returns false |
a<b | Less than | 5<8 returns true |
a>= b | Greater than OR equal to | 5>=8 returns false |
a<= b | Less than OR equal to | 5<=8 returns true |
Error Control Operators: "@" is known as error control. As if we use this operator then error will not be generator for that statement. See is example below.
$a=10; echo @$aa;
when we run able code, we are expecting the "$aa" should be generate error, but is not because we have use @ operator in front of $aa.
Increment / Decrement Operator: When we use this operator, variable value is increased in increment and value is decreased in
Operator | Name | Description |
---|---|---|
++$a | Pre-increment | Increments$a by one, then returns $a |
$a++ | Post-increment | Returns $a, then increments $a by one |
--$a | Pre-decrement | Decrements$a by one, then returns $a |
$a-- | Post-decrement | Returns $a, then decrements $a by one |
$a+=2 | Post-increment | Returns $a, then incremet $a by two |
$a+=3 | Post-increment | Returns $a, then increment $a by three |
$a-=2 | Post-decrement | Returns $a, then decrements $a by two |
Logical Operator: Logical Operator are used to compare the two string/number.
Example | Name | Result |
---|---|---|
$a and $b | And | TRUE if both $a and $b are TRUE. |
$a or $b | Or | TRUE if either $a or $b is TRUE. |
$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. |
String Operators: The operator which work on only on string. For Example "." and ".=" is known as string operator. 1st used to concat two or more strings. 2nd is work same but return the values.
for more detail read above.
Array Operators: The Operator which work for array known as array operator. Array operator are very useful while you are developing any website. Most of developer are unware of this. instead of using these function they created a function which work like this.
Means If you know about these operator, it will save your time and also minimize the code.
Example | Name | Result |
$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. |
Type Operator: "instanceOf" is type of operator which is used to check whether object is belong to that class or not. See below example.
"var_dump" return output alongwith their data type.
/** create TestClass **/ class TestClass{ function name(){ return 'Im Good'; } } /** create TestClass **/ $a = new TestClass; /** create TestClass1 **/ class TestClass1 { } /** create TestClass1 **/ var_dump($a instanceof TestClass); var_dump($a instanceof TestClass1);
Execution Operator: "`" is known as execution operator. Backticks(``) is a operator which is used to execute the code.
Following things keep in mind regarding this topic(Minimum)
- What are different types of operator
- Operator Precedence and their associations.
- Difference between bitwise operator, logical operator and arithmetic operators.
- How backets changes the precedence of operators
No comments:
Post a Comment