Articles → PHP → Operators In PHP
Operators In PHP
Purpose
Types Of Operators
- Arithmetic operators
- Assignment operators
- Incrementing/Decrementing operators
- Comparison operators
- Logical operators
Arithmetic Operators
Operator | Name | Description |
---|
+ | Add | The sum of 2 operands (left operand + right operand) |
- | Subtract | Difference of 2 operands (left operand – right operand) |
* | Multiply | Product of 2 operands (left operand * right operand) |
/ | Division | The quotient of 2 operands (left operand / right operand) |
% | Modulus | The remainder of division when one operand is divided by other |
- Operand | Negation | Opposite of operand |
. | Concatenation | Combine 2 strings into one. |
<?PHP
echo "Addition", "\n", 1 + 2, "<br>";
echo "Subtract", "\n", 2 - 1, "<br>";
echo "Multiplication", "\n", 2 * 3, "<br>";
echo "Division", "\n", 4 * 2, "<br>";
echo "Modulus", "\n", 5 % 3, "<br>";
echo "Negation", "\n", -3, "<br>";
echo "Concatenation", "\n", "wel" . "come", "<br>";
?>
Assignment Operators
Operators | How they are used |
---|
a = b | a = b |
a += b | a = a + b |
a -= b | a = a + b |
a *= b | a = a * b |
a /= b | a = a / b |
a %= b | a = a % b |
<?php
$a = 1;
$b = 2;
$a = $b;
echo $a, "\n";
$a += $b;
echo $a, "\n";
$a -= $b;
echo $a, "\n";
$a *= $b;
echo $a, "\n";
$a /= $b;
echo $a, "\n";
$a %= $b;
echo $a, "\n";
?>
Incrementing/Decrementing Operators
Operators | Description |
---|
++x | This operator increments the value of the variable by one and then returns it. |
x++ | This operator returns the variable and then increments it by one. |
--y | This operator decrements the value of the variable by one and then returns it. |
y-- | This operator returns the variable and then decrements it by one. |
<?php
$var = 4;
echo ++$var;
echo $var++;
echo --$var;
echo $var--;
?>
- Value of the variable "$var" is initialized with 4
- In the first echo statement, the value of the variable is incremented by one (value becomes 5) and then displays the value
- In the second echo statement the value of the variable is displayed first then increments by one. At the time of display, the value of the variable is 5 and after that, it has been incremented to 6
- In the third echo statement, the value of the variable is first decremented by one and then displays on the screen. After the last operation the value was incremented to 6 and now after the value is decremented the value of the variable becomes 5 again. So, the third echo statement also displays the value as 5
- In the last echo statement, the value is first displayed and then decrements. So, the last echo statement also displays 5
Comparison Operators
Operators | Description |
---|
a == b | Checks if the value of a is equal to the value of b even if the data type is different than this expression returns true else returns false |
a === b | Checks if the value of a is equal to the value of b and the data type of both the operands are the same then this expression returns true else false |
a != b Or a <> b | If the value of a is not equal to the value of b, then this expression returns true else false |
a !== b | If the value of a is not equal to the value of b or the data type of a is not equal to the data type of b then this expression returns true else false |
a > b | If the value of a is greater than the value of b then this expression returns true else false |
a < b | If the value of a is less than the value of b then this expression returns true else false |
a >= b | If the value of a is greater than or equal to the value of b then this expression returns true else false |
a <= b | If the value of a is less than or equal to the value of b then this expression returns true else false |
Logical Operators
Operators | Name | Description |
---|
x and y / x && y | And | True of both x and y are true |
x or y / x || y | Or | True if either one or both is true |
x xor y | Xor | True if either one is true but not both |
!x | Not | True if x is not true |