1 of 2

Notes – Operators in PHP

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 TypeDescription
Arithmetic OperatorsPerform mathematical operations
Comparison OperatorsCompare two values and return a boolean result
Logical OperatorsCombine multiple conditions
Assignment OperatorsAssign values to variables
Increment/DecrementIncrease or decrease variable values
Bitwise OperatorsPerform operations on bits
String OperatorsConcatenate and manipulate strings
Error Control OperatorsSuppress error messages

Arithmetic Operators


OperatorMeaningExampleResult
+Addition$a + $bSum
-Subtraction$a - $bDifference
*Multiplication$a * $bProduct
/Division$a / $bQuotient
%Modulus$a % $bRemainder

Comparison Operators


OperatorMeaningExampleResult
==Equal to$a == $btrue or false
===Identical (same type)$a === $btrue or false
!=Not equal to$a != $btrue or false
!==Not identical$a !== $btrue or false
>Greater than$a > $btrue or false
<Less than$a < $btrue or false
>=Greater than or equal$a >= $btrue or false
<=Less than or equal$a <= $btrue or false

Logical Operators


OperatorMeaningExampleResult
&&AND$a && $btrue or false
||OR$a || $btrue or false
!NOT!$atrue or false

Assignment Operators


OperatorMeaningExampleResult
=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


OperatorMeaningExampleResult
++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


OperatorMeaningExampleResult
&AND$a & $bBitwise AND
|OR$a | $bBitwise OR
^XOR$a ^ $bBitwise XOR
~NOT~$aBitwise NOT
<<Left Shift$a << 1Shift bits left
>>Right Shift$a >> 1Shift bits right

String Operators


OperatorMeaningExampleResult
.Concatenation$a . $bConcatenates $a and $b
.=Concatenate and assign$a .= $bAppends $b to $a

Error Control Operators

The error control operator @ suppresses error messages generated by expressions.

$result = @file('nonexistentfile.txt');