PHP Operators
An operator is a symbol that operates on a value or a variable. PHP has a wide range of operators to perform different operations for solving problems. Eg: + is an operator to perform addition.
Arithmetic Operators
An arithmetic operator performs basic mathematical operations such as addition, subtraction, multiplication and division on variables (and constants).
Operator | Operation | Example | Description |
---|---|---|---|
+ | Addition | $x + $y | Sum of operands |
- | Subtraction | $x - $y | Difference of operands |
* | Multiplication | $x * $y | Product of operands |
/ | Division | $x / $y | Quotient of operands |
% | Modulus | $x % $y | Remainder of operands |
Increment and Decrement Operators
PHP has two operators, increment (++) and decrement (--) to change the value of an operand (constant or variable) by 1. Increment (++) increases the value by 1 whereas decrement (--) decreases the value by 1.
Operator | Operation | Example | Description |
---|---|---|---|
++ | Increment | $x++, ++$x | Increment the value by 1 |
-- | Decrement | $x--, --$x | Decrement the value by 1 |
Assignment Operators
PHP assignment operator is used for assigning a value to the variable. The most common assignment operator is '='.
Operator | Operation | Example | Description |
---|---|---|---|
= | Assign | $x = $y | The value of the right operand is assigned to the left operand. |
+= | Add then Assign | $x += $y | Same as $x = $x + $y |
-= | Subtract then Assign | $x -= $y | Same as $x = $x - $y |
*= | Multiply then Assign | $x *= $y | Same as $x = $x * $y |
/= | Divide then Assign | $x /= $y | Same as $x = $x / $y |
%= | Divide then Assign | $x %= $y | Same as $x = $x % $y |
Comparison Operators
A comparison operator compares two operands. If the comparison is valid, it returns true; if the comparison is incorrect, it returns false. So comparison operators return Boolean values.
Operator | Operation | Example | Description |
---|---|---|---|
== | Equals | $x == $y | Returns True if both the operands are equal |
=== | Identical | $x === $y | Returns True if both the operands are equal and are of the same type |
!== | Not identical | $x !== $y | Returns TRUE if $x is not equal to $y, and they are not of same data type |
!= | Not equal | $x != $y | Returns TRUE if $x is not equal to $y |
<> | Not equal | $x <> $y | Returns True if both the operands are unequal |
< | Less than | $x < $y | Returns TRUE if $x is less than $y |
> | Greater than | $x < $y | Returns TRUE if $x is greater than $y |
<= | Less than or equal to | $x <= $y | Returns TRUE if $x is less than or equal to $y |
>= | Greater than or equal to | $x >= $y | Returns TRUE if $x is greater than or equal to $y |
Logical Operators
PHP logical operators are used to combine conditional statements. Logical operators first convert their operands to boolean values and then perform the respective comparison. The result of a logical operation is either 0 or 1 (True or False).
Operator | Operation | Example | Description |
---|---|---|---|
and | AND | $x and $y | Returns TRUE if both $x and $y are true, otherwise FALSE |
or | OR | $x or $y | Returns TRUE if either of the operands is true, else FALSE |
xor | XOR | $x xor $y | Returns TRUE if either $x or $y is true, false if both are true |
! | NOT | ! $x | Returns TRUE if $x is not true |
&& | AND | $x && $y | Return TRUE if both the operands are true, else FALSE |
|| | OR | $x || $y | Returns TRUE if either $x or $y is true |
Logical Operators
Conditional Operator is used to perform a simple comparison or check on a condition having simple statements. It is an alternative method of implementing an if-else statement. It is called a ternary operator because it takes three operands:- a condition, a result for true, and a result for false.
Syntax:
(Condition) ? (Statement1) : (Statement2);
Eg:
<?php
$marks = 55;
echo ($marks >= 50) ? "Passed" : " Failed"; // Output will be:- Passed
?>