PHP supports many kinds of operators:
- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Comparison Operators
- Increment/Decrement Operators
- Logical Operators
- Concatenating Operators
Arithmetic Operators
Operator | Description |
---|---|
+ | Return the sum of two operands |
– | Return the difference between two operands |
* | Return the product of two operands |
/ | Return the quotient of two operands |
% | Return the remainder of the division of the first operand by the second one |
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php $x = 20; $y = 10; // add, subtract, and multiplication operators demo echo $x + $y . '<br/>'; // 30 echo $x - $y . '<br/>'; // 10 echo $x * $y . '<br/>'; // 200 // division operator demo $z = $x / $y; echo gettype($z) . '<br/>'; // integer $z = $y / $x; echo gettype($z) . '<br/>'; // double // modulus demo $y = 15; echo $x % $y . '<br/>'; // 5 |
Assignment Operators
Operator | Syntax | Operation |
---|---|---|
= | $x = $y | Operand on the left obtains the value of the operand on the right |
+= | $x += $y | Simple Addition same as $x = $x + $y |
-= | $x -= $y | Simple subtraction same as $x = $x – $y |
*= | $x *= $y | Simple product same as $x = $x * $y |
/= | $x /= $y | Simple division same as $x = $x / $y |
%=< | $x %= $y | Simple division same as $x = $x % $y |
Comparison Operators
Comparison operators allow you to compare two operands.
A comparison operator returns a Boolean value, either true
or false
. If the comparison is truthful, the comparison operator returns true
, otherwise, it returns false
.
Operator | Description |
---|---|
== | Return true if both operands are equal, otherwise returns false . |
=== | Return true if both operands have the same data type and equal, otherwise return false . |
!=== | Return true if both operands are not equal or not have the same data type, otherwise return false . |
> | Return true if the operand on the left is greater thanthe operand on the right, otherwise return false . |
>= | Return true if the operand on the left is greater than orequal to the operand on the right, otherwise return false . |
< | Return true if the operand on the left is less than theoperand on the right, otherwise return false . |
<= | Return true if the operand on the left is less than orequal to the operand on the right, otherwise return false . |
Logical Operators
Logical operators allow you to construct logical expressions. A logical operator returns a Boolean value.
Operator | Description |
---|---|
&& | Return true if both operands are true , otherwisereturn false . If the first operand is false , it will not evaluate the second operand because it knows for sure that the result is going to be false . This is known as short-circuiting. |
|| | Return true if one of the operands is true ,otherwise returns false . If the first operand istrue , it will not evaluate the second one. |
xor | Return true if either operand, not both, is true , otherwise, return false . |
! | returns true if the operand is false , andreturns false if the operand is true . |
Bitwise Operators
Bitwise operators perform operations on the binary representation of the operands.
Operators | Result |
---|---|
$x & $y | If both bits are 1, the corresponding bit in the result is 1; otherwise, the corresponding bit is 0 |
$x | $y | If both bits are 0, the corresponding bit in the result is 0; otherwise, the corresponding bit is 1 |
$x ^ $y | If either bit, but not both, in $x and$y are 1, the corresponding bit in the result is 1;otherwise, the corresponding bit is 0 |
~ $x | Change bit 1 to 0 and 0 to 1 in the $x operand |
$x << $y | Shifts the bits in $x left by the number of places specified by$y . |
$x >> $y | Shifts the bits in $x right by the number of placesspecified by $y . |
Incrementing/ Decrementing Operators
Increment (++) and decrement (–) operators give you a quick way to increase and decrease the value of a variable by 1.
Example | Returned Value | Effect on $a |
---|---|---|
++$a | $a + 1 | Increments $a by 1, then returns $a . |
$a++ | $a | Returns $a , then increments $a by 1. |
--$a | $a - 1 | Decrements $a by 1, then returns $a . |
$a-- | $a | Returns $a , then decrements $a by 1. |
Concatenating Operator
Concatenating operator (.) allows you to combine two strings into one. It appends the second string to the first one and returns the combined string.
Ex:
1 2 3 | <?php $str = 'PHP' . ' is ' . ' Awesome!'; echo $str; |