Operators in PHP are used to perform operations on variables and values.
PHP supports a wide range of operators to work with arithmetic, comparison, logical, and more.
Types of Operators in PHP
Operator Type | Description |
---|
Arithmetic Operators | Perform mathematical operations |
Comparison Operators | Compare two values and return a boolean result |
Logical Operators | Combine multiple conditions |
Assignment Operators | Assign values to variables |
Increment/Decrement | Increase or decrease variable values |
Bitwise Operators | Perform operations on bits |
String Operators | Concatenate and manipulate strings |
Error Control Operators | Suppress error messages |
Arithmetic Operators
Operator | Meaning | Example | Result |
---|
+ | Addition | $a + $b | Sum |
- | Subtraction | $a - $b | Difference |
* | Multiplication | $a * $b | Product |
/ | Division | $a / $b | Quotient |
% | Modulus | $a % $b | Remainder |
Comparison Operators
Operator | Meaning | Example | Result |
---|
== | Equal to | $a == $b | true or false |
=== | Identical (same type) | $a === $b | true or false |
!= | Not equal to | $a != $b | true or false |
!== | Not identical | $a !== $b | true or false |
> | Greater than | $a > $b | true or false |
< | Less than | $a < $b | true or false |
>= | Greater than or equal | $a >= $b | true or false |
<= | Less than or equal | $a <= $b | true or false |
Logical Operators
Operator | Meaning | Example | Result |
---|
&& | AND | $a && $b | true or false |
|| | OR | $a || $b | true or false |
! | NOT | !$a | true or false |
Assignment Operators
Operator | Meaning | Example | Result |
---|
= | Assign value | $a = 5; | Assigns value 5 to $a |
+= | Add and assign | $a += 3; | $a = $a + 3 |
-= | Subtract and assign | $a -= 2; | $a = $a - 2 |
*= | Multiply and assign | $a *= 4; | $a = $a * 4 |
/= | Divide and assign | $a /= 2; | $a = $a / 2 |
Increment/Decrement Operators
Operator | Meaning | Example | Result |
---|
++ | Increment by 1 | $a++ | a = a + 1 |
-- | Decrement by 1 | $a-- | a = a - 1 |
Pre-increment (++$a
): Increases value before using it
Post-increment ($a++
): Increases value after using it
Bitwise Operators
Operator | Meaning | Example | Result |
---|
& | AND | $a & $b | Bitwise AND |
| | OR | $a | $b | Bitwise OR |
^ | XOR | $a ^ $b | Bitwise XOR |
~ | NOT | ~$a | Bitwise NOT |
<< | Left Shift | $a << 1 | Shift bits left |
>> | Right Shift | $a >> 1 | Shift bits right |
String Operators
Operator | Meaning | Example | Result |
---|
. | Concatenation | $a . $b | Concatenates $a and $b |
.= | Concatenate and assign | $a .= $b | Appends $b to $a |
Error Control Operators
The error control operator @
suppresses error messages generated by expressions.
$result = @file('nonexistentfile.txt');