PHP Operators are the special symbols or strings for operations on PHP variables & values. Discover the different categories of PHP Operators with simple programming code examples:
In this tutorial, you will learn about PHP operators (like PHP assignment operators, PHP comparison operators, PHP ternary operators, and many more), the precedence of operators, and frequently asked questions (FAQs).
Please note that we have used PHP version 7 in all examples.
Let’s begin!
Table of Contents:
Understanding PHP Operators

What is an Operator
In simple words, an operator is a symbol that is used to operate on operands. When you consider the example, 5 + 10, 5 & 10 are the operands, and + is the operator.
Classification of PHP Operators
There are 11 categories of operators in PHP. They are:
- Arithmetic
- Assignment
- Comparison
- Incrementing and decrementing
- Bitwise
- Logical
- String
- Array
- Type
- Execution
- Error Control
Apart from the above classification, we can also classify operators based on operands as shown in the list below:
- PHP Unary Operators – It works on a single operand. Example: ++, —
- PHP Binary Operators – It works on two operands. Example: +, –
- PHP Ternary Operators – It works on three operands. Example: ?:
#1) Arithmetic Operator
An arithmetic operator is used to perform common arithmetic operations like addition, multiplication, etc.
The table below shows more details:
| Symbol | Name | Syntax | Description | |
|---|---|---|---|---|
| 1 | + | Addition | $c + $d | Sum of $c and $d |
| 2 | – | Subtraction | $c – $d | Difference of $c and $d |
| 3 | * | Multiplication | $c * $d | Product of $c and $d |
| 4 | / | Division | $c / $d | Quotient of $c and $d |
| 5 | % | Modulo | $c % $d | Remainder of $c divided by $d |
| 6 | ** | Exponentiation | $c ** $d | Result of raising $c to the $d’th power |
| 7 | + | Identity | +$c | Conversion of $c to int or float as appropriate |
| 8 | – | Negation | -$c | Opposite of $c |
Example:
The below programming code shows an example. You can try this example by running the following code:
<?php
$c = 2;
$d = 4;
echo $c + $d."<br>"; //outputs: 6
echo $c - $d."<br>"; //outputs: -2
echo $c * $d."<br>"; //outputs: 8
echo $c / $d."<br>"; //outputs: 0.5
echo $c % $d."<br>"; //outputs: 2
echo $c ** $d."<br>"; //outputs: 16
echo +$c."<br>"; //outputs: 2
echo -$c; //outputs: -2
?>
#2) Assignment Operator
Assignment operators are used to assign values to variables. The most fundamental assignment operator is “=”.
The table below shows more details:
| Symbol | Sub-category | Syntax | Same As | Description | |
|---|---|---|---|---|---|
| 1 | = | – | $c = $d | $c = $d (the value of the right operand is assigned to the left operand) | Assign |
| 2 | += | Arithmetic Assignment | $c += $d | $c = $c + $d | Addition |
| 3 | -= | Arithmetic Assignment | $c -= $d | $c = $c – $d | Subtraction |
| 4 | *= | Arithmetic Assignment | $c *= $d | $c = $c * $d | Multiplication |
| 5 | /= | Arithmetic Assignment | $c /= $d | $c = $c / $d | Division |
| 6 | %= | Arithmetic Assignment | $c %= $d | $c = $c % $d | Modulus |
| 7 | **= | Arithmetic Assignment | $c **= $d | $c = $c ** $d | Exponentiation |
| 8 | &= | Bitwise Assignment | $c &= $d | $c = $c & $d | Bitwise And |
| 9 | |= | Bitwise Assignment | $c |= $d | $c = $c | $d | Bitwise Or |
| 10 | ^= | Bitwise Assignment | $c ^= $d | $c = $c ^ $d | Bitwise Xor |
| 11 | <<= | Bitwise Assignment | $c <<= $d | $c = $c << $d | Left Shift |
| 12 | >>= | Bitwise Assignment | $c >>= $d | $c = $c >> $d | Right Shift |
| 13 | .= | – | $c .= $d | $c = $c . $d | String Concatenation |
| 14 | ??= | – | $c ??= $d | $c = $c ?? $d | Null Coalesce |
Example:
The below programming code shows an example. You can try this example by running the following code. Output values are shown in the comments.
<?php
$c = 2;
$d = 4;
echo ($c = $d)."<br>"; //outputs: 4
$c = 2;
$d = 4;
echo ($c += $d)."<br>"; //outputs: 6
$c = 2;
$d = 4;
echo ($c -= $d)."<br>"; //outputs: -2
$c = 2;
$d = 4;
echo ($c *= $d)."<br>"; //outputs: 8
$c = 2;
$d = 4;
echo ($c /= $d)."<br>"; //outputs: 0.5
$c = 2;
$d = 4;
echo ($c %= $d)."<br>"; //outputs: 2
$c = 2;
$d = 4;
echo ($c **= $d)."<br>"; //outputs: 16
$c = 2;
$d = 4;
echo ($c &= $d)."<br>"; //outputs: 0
$c = 2;
$d = 4;
echo ($c |= $d)."<br>"; //outputs: 6
$c = 2;
$d = 4;
echo ($c ^= $d)."<br>"; //outputs: 6
$c = 2;
$d = 4;
echo ($c <<= $d)."<br>"; //outputs: 32
$c = 2;
$d = 4;
echo ($c >>= $d)."<br>"; //outputs: 0
$c = 2;
$d = 4;
echo ($c .= $d)."<br>"; //outputs: 24
$c = 2;
$d = 4;
echo ($c ??= $d); //outputs: 2
?>
#3) Comparison Operator
Comparison operators are used to compare values.
The table below shows more details:
| Symbol | Name | Syntax | Description | |
|---|---|---|---|---|
| 1 | == | Equal | $c == $d | Return TRUE if $c is equal to $d |
| 2 | === | Identical | $c === $d | Return TRUE if $c is equal to $d, and they are of the same data type |
| 3 | != | Not equal | $c != $d | Return TRUE if $c is not equal to $d |
| 4 | <> | Not equal | $c <> $d | Return TRUE if $c is not equal to $d |
| 5 | !== | Not identical | $c !== $d | Return TRUE if $c is not equal to $d, and they are not of the same data type |
| 6 | < | Less than | $c < $d | Return TRUE if $c is less than $d |
| 7 | > | Greater than | $c > $d | Return TRUE if $c is greater than $d |
| 8 | <= | Less than or equal to | $c <= $d | Return TRUE if $c is less than or equal to $d |
| 9 | >= | Greater than or equal to | $c >= $d | Return TRUE if $c is greater than or equal to $d |
| 10 | <=> | Spaceship | $c <=>$d | Return -1, 0 or 1 when $c is less than, equal to, or greater than $d respectively. |
Note: The following two operators are also referred to as PHP comparison operators.
- The ternary operator (?:)
- The null coalescing operator (??)
Example:
The below programming code shows an example. You can try this example by running the following code. Output values are shown in the comments.
In the below example, 50 is an integer, and “50” is a string.
<?php
$c = 50;
$d = "50";
var_dump($c == $d); //outputs: bool(true)
echo "<br>";
var_dump($c === $d); //outputs: bool(false)
echo "<br>";
var_dump($c != $d); //outputs: bool(false)
echo "<br>";
var_dump($c !== $d); //outputs: bool(true)
echo "<br>";
var_dump($c <> $d); //outputs: bool(false)
echo "<br>";
var_dump($c < $d); //outputs: bool(false)
echo "<br>";
var_dump($c > $d); //outputs: bool(false)
echo "<br>";
var_dump($c <= $d); //outputs: bool(true)
echo "<br>";
var_dump($c >= $d);//outputs: bool(true)
echo "<br>";
var_dump($c <=>$d); //outputs: int(0)
?>
#4) Incrementing and Decrementing Operators
Incrementing and decrementing operators are used to increase and decrease the value of a variable.
The table below shows more details:
| Symbol | Name | Syntax | Description | |
|---|---|---|---|---|
| 1 | ++ | Pre-increment | ++$c | Increment the value of $c by one and then return $c |
| 2 | ++ | Post-increment | $c++ | Return $c (and then increment the value of $c by one) |
| 3 | — | Pre-decrement | –$c | Decrement the value of $c by one and then return $c |
| 4 | — | Post-decrement | $c– | Return $c (and then decrement the value of $c by one) |
Example:
The below programming code is an example. Output values are shown in the comments.
<?php
$c = 50;
echo ++$c."<br>"; //outputs: 51
$c = 50;
echo $c++."<br>"; //outputs: 50
$c = 50;
echo --$c."<br>"; //outputs: 49
$c = 50;
echo $c--; //outputs: 50
?>
#5) Bitwise Operator
A bitwise operator is used to perform bit-level operations.
The table below shows more details:
| Symbol | Name | Syntax | Description | |
|---|---|---|---|---|
| 1 | & | Bitwise And | $c & $d | Bits that are set in both $c and $d are set. |
| 2 | | | Bitwise Or (Inclusive Or) | $c | $d | Bits that are set in either $c or $d are set. |
| 3 | ^ | Bitwise Xor (Exclusive Or) | $c ^ $d | Bits that are set in $c or $d but not both are set. |
| 4 | ~ | Bitwise Not | ~$c | Bits that are set in $c are not set, and vice versa. |
| 5 | << | Bitwise Left Shift | $c << $d | Shift the bits of $c $d steps to the left (each step means “multiply by two”) |
| 6 | >> | Bitwise Right Shift | $c >> $d | Shift the bits of $c $d steps to the right (each step means “divide by two”) |
Example:
The below programming code shows an example. You can try this example by running the following code. Output values are shown in the comments.
<?php
$c = 2;
$d = 4;
echo($c & $d)."<br>"; //outputs: 0
echo($c | $d)."<br>"; //outputs: 6
echo($c ^ $d)."<br>"; //outputs: 6
echo(~$c)."<br>"; //outputs: -3
echo($c << $d)."<br>"; //outputs: 32
echo($c >> $d); //outputs: 0
?>
#6) Logical Operator
A logical operator is used to integrate conditional statements.
The table below shows more details:
| Symbol | Name | Syntax | Description | |
|---|---|---|---|---|
| 1 | and | And | $c and $d | Return TRUE if both $c and $d are true |
| 2 | or | Or | $c or $d | Return TRUE if either $c or $d is true |
| 3 | xor | Xor | $c xor $d | Return TRUE if either $c or $d is true, but not both |
| 4 | ! | Not | !$c | Return TRUE if $c is not true |
| 5 | && | And | $c && $d | Return TRUE if both $c and $d are true |
| 6 | || | Or | $c || $d | Return TRUE if either $c or $d is true |
Example:
The below programming code shows an example. Output values are shown in the comments.
<?php
$c = true;
$d = false;
var_dump($c and $d); //outputs: bool(false)
echo "<br>";
var_dump($c or $d); //outputs: bool(true)
echo "<br>";
var_dump($c xor $d); //outputs: bool(true)
echo "<br>";
var_dump(!$c); //outputs: bool(false)
echo "<br>";
var_dump($c && $d); //outputs: bool(false)
echo "<br>";
var_dump($c || $d); //outputs: bool(true)
?>
#7) String Operator
A string operator is used to perform operations on strings.
The table below shows more details:
| Symbol | Name | Syntax | Description | |
|---|---|---|---|---|
| 1 | . | Concatenation | $c . $d | Concatenate $c and $d |
| 2 | .= | Concatenating assignment | $c .= $d | Appends $d to $c |
Example:
The below programming code shows an example. You can try this example by running the following code. Output values are shown in the comments.
$c = "My";
$d = " Home";
echo $c . $d ."<br>"; //outputs: My Home
echo $c .= $d; //outputs: My Home
#8) Array Operator
They are used to performing operations on arrays. They are usually comparing the values of arrays.
The table below shows more details:
| Symbol | Name | Syntax | Description | |
|---|---|---|---|---|
| 1 | + | Union | $c + $d | Union of $c and $d |
| 2 | == | Equality | $c == $d | Return TRUE if $c and $d have the same key-value pair |
| 3 | === | Identity | $c === $d | Return TRUE if $c and $d have the same key-value pair in the same order and of the same type |
| 4 | != | Inequality | $c != $d | Return TRUE if $c is not equal to $d |
| 5 | <> | Inequality | $c <> $d | Return TRUE if $c is not equal to $d |
| 6 | !== | Non-Identity | $c !== $d | Return TRUE if $c is not identical to $d |
Example:
The below programming code shows an example. Output values are shown in the comments.
<?php
$c = array("a" => "Red", "b" => "Yellow", "c" => "Green");
$d = array("d" => "Pink", "e" => "Orange", "f" => "Blue");
var_dump($c + $d);
echo "<br>"; //outputs: array(6) { ["a"]=> string(3) "Red" ["b"]=> string(6) "Yellow" ["c"]=> string(5) "Green" ["d"]=> string(4) "Pink" ["e"]=> string(6) "Orange" ["f"]=> string(4) "Blue" }
var_dump($c == $d); //outputs: bool(false)
echo "<br>";
var_dump($c === $d); //outputs: bool(false)
echo "<br>";
var_dump($c != $d); //outputs: bool(true)
echo "<br>";
var_dump($c <> $d); //outputs: bool(true)
echo "<br>";
var_dump($c !== $d); //outputs: bool(true)
?>
#9) Type Operator
PHP has a type operator called instanceof, and its function is to determine which class an object belongs to. It is used in object-oriented programming.
Example:
The below programming code shows an example. You can try this example by running the following code. Output values are shown in the comments.
<?php
class AnimalClass
{
}
class NotAnimalClass
{
}
$a = new AnimalClass;
var_dump($a instanceof AnimalClass); //outputs: bool(true)
var_dump($a instanceof NotAnimalClass); //outputs: bool(false)
?>
#10) Execution Operator
PHP has only one execution operator, backticks (“). PHP executes the content inside the backticks as a shell command. It is similar to the shell_exec() function, and both give the same result.
Note: Backticks (“) are not single quotes.
Example:
The below programming code shows an example. You can try this example by running the following code:
<?php
$list=`dir`;
echo "$list";
?>
It will show available directories in the current directory as the output.
#11) Error Control Operator
PHP has only one error control operator, the at symbol (@). When you prefix it to any expression, any error message encountered by the expression will be ignored.
Example:
The following example elaborates on the functionality of the error control operator. You can try this example by running the following code. Assume that the file, missingfile.txt does not exist.
- Code A – Without @
<?php
echo "My File";
$fp=fopen("missingfile.txt","r");
?>
The following text shows the browser output of the above programming code. As there is no @, the error will be displayed.
My File Warning: fopen(missingfile.txt): Failed to open stream: No such file or directory in C:\xampp\htdocs\test.php on line 3
- Code B – With @
<?php
echo "My File";
$fp=@fopen("missingfile.txt","r");
?>
The text below shows the browser output of the above programming code. As there is @ before fopen, the error will not be displayed.
My File
Precedence of Operators
The operator precedence determines the order in which operators are going to executed.
The following table shows the operators in order of precedence from the highest precedence (at the top) to the lowest precedence (at the bottom). Furthermore, the operators on the same line have equal precedence.
| Operator | Description | Associativity | |
|---|---|---|---|
| 1 | clone new | clone and new | (n/a) |
| 2 | ** | Arithmetic | right |
| 3 | + – ++ — ~ (int) (float) (string) (array) (object) (bool) @ | Arithmetic (unary +) Arithmetic (unary -) Increment Decrement Bitwise Type Type Type Type Type Type Error control | (n/a) |
| 4 | instanceof | Type | Left |
| 5 | ! | Logical | (n/a) |
| 6 | * / % | Arithmetic Arithmetic Arithmetic | Left |
| 7 | + – . | Arithmetic (binary +) Arithmetic (binary -) String | Left |
| 8 | << >> | Bitwise Bitwise | Left |
| 9 | < <= > >= | Comparison Comparison Comparison Comparison | Non-associative |
| 10 | == != === !== <> <=> | Comparison Comparison Comparison Comparison Comparison Comparison | Non-associative |
| 11 | & | Bitwise and reference | Left |
| 12 | ^ | Bitwise | Left |
| 13 | | | Bitwise | Left |
| 14 | && | Logical | Left |
| 15 | || | Logical | Left |
| 16 | ?? | Null coalescing | Right |
| 17 | ?: | Ternary | Left associative (Non-associative in PHP 8.0.0) |
| 18 | = += -= *= **= /= .= %= &= |= ^= <<= >>= ??= | Assignment Assignment Assignment Assignment Assignment Assignment Assignment Assignment Assignment Assignment Assignment Assignment Assignment Assignment | Right |
| 19 | yield from | Yield from | (n/a) |
| 20 | yield | Yield | (n/a) |
| 21 | (n/a) | ||
| 22 | and | Logical | Left |
| 23 | xor | Logical | Left |
| 24 | or | Logical | Left |
Frequently Asked Questions
In this section, we will discuss some of the FAQs related to this topic. These questions will help you prepare for your examinations and interviews confidently.
1. What are PHP operators?
They are symbols used to perform operations on operands.
2. How many operators are there in PHP?
There are 11 operators in PHP.
3. Does PHP have ++?
Yes, they are called incrementing operators.
4. Is PHP += a logical operator?
No, it is not. It is an assignment operator.
5. Are Bitwise operations faster than arithmetic operations?
Yes, they are usually faster than arithmetic operations.
6. What is the difference between == and === in PHP?
When you consider PHP == vs ===, PHP == is the equal operator that checks whether the two operands have the same value. On the other hand, PHP === is the identical operator that checks whether the two operands have the same value and the same type.
7. What is the difference between i++ and ++i in PHP?
++i increments the value of i by one and then returns i. Unlike ++i, i++ returns i first and then increments the value of i by one.
8. What are the main differences between bitwise AND and logical AND operators?
Bitwise AND is represented as “&” and logical AND is represented as “&&”. Logical AND works only on the boolean data type, but bitwise AND works on both boolean and other data types such as int, short, etc
Conclusion
PHP operators can be divided into 11 categories named arithmetic, assignment, comparison, incrementing/decrementing, bitwise, logical, string, array, type, execution, and error control operators.
The operator precedence determines the order in which operators are going to executed.






