What is Operators ?
Operators are used to perform operations on variables and values.
PHP Operator is a symbol i.e used to perform operations on operands.
For example: $num=10+20;//+ is the operator and 10,20 are operands
In the above example, + is the binary + operator, 10 and 20 are operands and $num is variable.
PHP Operators can be categorised in following forms:
- Arithmetic Operators
- Comparison Operators
- Bitwise Operators
- Logical Operators
- String Operators
- Incrementing/Decrementing Operators
- Array Operators
- Type Operators
- Execution Operators
- Error Control Operators
- Assignment Operators
Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
Name | Example | Result |
Identity | +$a | Conversion of $a to int or float as appropriate. |
Negation | -$a | Opposite of $a. |
Addition | $a + $b | Sum of $a and $b. |
Subtraction | $a – $b | Difference of $a and $b. |
Multiplication | $a * $b | Product of $a and $b. |
Division | $a / $b | Quotient of $a and $b. |
Modulo | $a % $b | Remainder of $a divided by $b. |
Exponentiation | $a ** $b | Result of raising $a to the $b‘th power. Introduced in PHP 5.6. |
Comparison Operators
The PHP comparison operators are used to compare two values (number or string).
Name | Example | Result |
Equal | $a ==$b | TRUE if $a is equal to $b after type juggling. |
Identical | $a===$b | TRUE if $a is equal to $b, and they are of the same type. |
Not equal | $a != $b | TRUE if $a is not equal to $b after type juggling. |
Not equal | $a <>$b | TRUE if $a is not equal to $b after type juggling. |
Not identical | $a!==$b | TRUE if $a is not equal to $b, or they are not of the same type. |
Less than | $a < $b | TRUE if $a is strictly less than $b. |
Greater than | $a > $b | TRUE if $a is strictly greater than $b. |
Less than or equal to | $a <= $b | TRUE if $a is less than or equal to $b. |
Greater than or equal to | $a >= $b | TRUE if $a is greater than or equal to $b. |
Spaceship | $<=>$b | An integer less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively. Available as of PHP 7. |
Bitwise Operators
Name | Example | Result |
And | $a & $b | Bits that are set in both $a and $b are set. |
Or (inclusive or) | $a | $b | Bits that are set in either $a or $b are set. |
Xor (exclusive or) | $a ^ $b | Bits that are set in $a or $b but not both are set. |
Not | ~$a | Bits that are set in $a are not set, and vice versa. |
Shift left | $a<< $b | Shift the bits of $a $b steps to the left (each step means “multiply by two”) |
Shift right | $a>> $b | Shift the bits of $a $b steps to the right (each step means “divide by two”) |
Logical Operators
The PHP logical operators are used to combine conditional statements.
Name | Example | Result |
---|---|---|
And | $a and $b | TRUE if both $a and $b are TRUE. |
Or | $a or $b | TRUE if either $a or $b is TRUE. |
Xor | $a xor $b | TRUE if either $a or $b is TRUE, but not both. |
Not | ! $a | TRUE if $a is not TRUE. |
And | $a && $b | TRUE if both $a and $b are TRUE. |
Or | $a || $b | TRUE if either $a or $b is TRUE. |
String Operators
There are two string operators. The first is the concatenation operator (‘.’), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (‘.=’), which appends the argument on the right side to the argument on the left side.
Name | Example | Result |
Concatenation | echo $str1.” ”.$str2; | Combine Two String Temporary |
Appends | $str1 .= $str2 echo $str1; | Combine Str1 and Str2 into String Str1. |
Incrementing/Decrementing Operators
The PHP decrement operators are used to decrement a variable’s value.
Name | Example | Effect |
Pre-increment | ++$a | Increments $a by one, then returns $a. |
Post-increment | $a++ | Returns $a, then increments $a by one. |
Pre-decrement | –$a | Decrements $a by one, then returns $a. |
Post-decrement | $a– | Returns $a, then decrements $a by one. |
Array Operators
The PHP array operators are used to compare arrays.
Name | Example | Result |
Union | $a + $b | Union of $a and $b. |
Equality | $a == $b | TRUE if $a and $b have the same key/value pairs. |
Identity | $a === $b | TRUE if $a and $b have the same key/value pairs in the same order and of the same types. |
Inequality | $a != $b | TRUE if $a is not equal to $b. |
Inequality | $a <> $b | TRUE if $a is not equal to $b. |
Non-identity | $a !== $b | TRUE if $a is not identical to $b. |
Type Operators
In Type Operator ” instanceof ” is used to determine whether a variable is an instantiated object of a certain class or Not.
Name | Example | Result |
instanceof | $a instanceof MyClass | TRUE if an instance object of a certain class |
Execution Operators
Execution Operators in php supports one execution operator: backticks (“). PHP will attempt to execute the contents of the backticks as a shell command.
<?php $output = `ls`; echo "<pre>$output</pre>"; /* Filehandle.php test.php test1.php test2.php */ ?>
Error Control Operators
Error Control Operators is used to skip error display in code.there is one error control operator with at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
<?php // Not Declare Variables $x = 10; echo $x; // 10 echo @$z; //nothing Show
Assignment operators
The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is “=”. It means that the left operand gets set to the value of the assignment expression on the right.
Operator Precedence
Associativity | Operators | Additional Information |
non-associative | clone new | clone and new |
right | ** | arithmetic |
right | ++ — ~ (int) (float) (string)(array) (object) (bool) @ | types and increment/decrement |
non-associative | instanceof | types |
right | ! | logical |
left | * / % | arithmetic |
left | + – . | arithmetic and string |
left | << >> | bitwise |
non-associative | < <= > >= | comparison |
non-associative | == != === !== <> <=> | comparison |
left | & | bitwise andreferences |
left | ^ | bitwise |
left | | | bitwise |
left | && | logical |
left | || | logical |
right | ?? | null coalescing |
left | ? : | ternary |
right | = += -= *= **= /= .= %= &= |=^= <<= >>= ??= | assignment |
right | yield from | yield from |
right | yield | yield |
left | and | logical |
left | xor | logical |
left | or | logical |