PHP Operators: Classification With Examples

By Vijay

By Vijay

Image
I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated July 4, 2025
Edited by Kamila

Edited by Kamila

Image
Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

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!

=> Complete PHP Guide for ALL

Understanding PHP Operators

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:

  1. Arithmetic
  2. Assignment
  3. Comparison
  4. Incrementing and decrementing
  5. Bitwise
  6. Logical
  7. String
  8. Array
  9. Type
  10. Execution
  11. 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:

SymbolNameSyntaxDescription
1+Addition$c + $dSum of $c and $d
2Subtraction$c – $dDifference of $c and $d
3*Multiplication$c * $dProduct of $c and $d
4/Division$c / $dQuotient of $c and $d
5%Modulo$c % $dRemainder of $c divided by $d
6**Exponentiation$c ** $dResult of raising $c to the $d’th power
7+Identity+$cConversion of $c to int or float as appropriate
8Negation-$cOpposite 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:

SymbolSub-categorySyntaxSame AsDescription
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 + $dAddition
3-=Arithmetic Assignment$c -= $d$c = $c – $dSubtraction
4*=Arithmetic Assignment$c *= $d$c = $c * $dMultiplication
5/=Arithmetic Assignment$c /= $d$c = $c / $dDivision
6%=Arithmetic Assignment$c %= $d$c = $c % $dModulus
7**=Arithmetic Assignment$c **= $d$c = $c ** $dExponentiation
8&=Bitwise Assignment$c &= $d$c = $c & $dBitwise And
9|=Bitwise Assignment$c |= $d$c = $c | $dBitwise Or
10^=Bitwise Assignment$c ^= $d$c = $c ^ $dBitwise Xor
11<<=Bitwise Assignment$c <<= $d$c = $c << $dLeft Shift
12>>=Bitwise Assignment$c >>= $d$c = $c >> $dRight Shift
13.=$c .= $d$c = $c . $dString Concatenation
14??=$c ??= $d$c = $c ?? $dNull 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:

SymbolNameSyntaxDescription
1 == Equal$c == $dReturn TRUE if $c is equal to $d
2 === Identical$c === $dReturn TRUE if $c is equal to $d, and they are of the same data type
3!=Not equal$c != $dReturn TRUE if $c is not equal to $d
4<>Not equal$c <> $dReturn TRUE if $c is not equal to $d
5!==Not identical$c !== $dReturn TRUE if $c is not equal to $d, and they are not of the same data type
6<Less than$c < $dReturn TRUE if $c is less than $d
7>Greater than$c > $dReturn TRUE if $c is greater than $d
8<=Less than or equal to$c <= $dReturn TRUE if $c is less than or equal to $d
9>=Greater than or equal to$c >= $dReturn TRUE if $c is greater than or equal to $d
10<=>Spaceship$c <=>$dReturn -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:

SymbolNameSyntaxDescription
1++Pre-increment++$cIncrement 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)
3Pre-decrement–$cDecrement the value of $c by one and then return $c
4Post-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:

SymbolNameSyntaxDescription
1&Bitwise And$c & $dBits that are set in both $c and $d are set.
2|Bitwise Or (Inclusive Or)$c | $dBits that are set in either $c or $d are set.
3^Bitwise Xor (Exclusive Or)$c ^ $dBits that are set in $c or $d but not both are set.
4~Bitwise Not~$cBits that are set in $c are not set, and vice versa.
5<<Bitwise Left Shift$c << $dShift the bits of $c $d steps to the left (each step means “multiply by two”)
6>>Bitwise Right Shift$c >> $dShift 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:

SymbolNameSyntaxDescription
1andAnd$c and $dReturn TRUE if both $c and $d are true
2orOr$c or $dReturn TRUE if either $c or $d is true
3xorXor$c xor $dReturn TRUE if either $c or $d is true, but not both
4!Not!$cReturn TRUE if $c is not true
5&&And$c && $dReturn TRUE if both $c and $d are true
6||Or$c || $dReturn 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:

SymbolNameSyntaxDescription
1.Concatenation$c . $dConcatenate $c and $d
2.=Concatenating assignment$c .= $dAppends $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:

SymbolNameSyntaxDescription
1+Union$c + $dUnion of $c and $d
2 ==Equality$c == $dReturn TRUE if $c and $d have the same key-value pair
3 ===Identity$c === $dReturn TRUE if $c and $d have the same key-value pair in the same order and of the same type
4!=Inequality$c != $dReturn TRUE if $c is not equal to $d
5<>Inequality$c <> $dReturn TRUE if $c is not equal to $d
6!==Non-Identity$c !== $dReturn 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.

OperatorDescriptionAssociativity
1clone newclone and new(n/a)
2**Arithmeticright
3+

++

~
(int)
(float)
(string)
(array)
(object)
(bool)
@
Arithmetic (unary +)
Arithmetic (unary -)
Increment
Decrement
Bitwise
Type
Type
Type
Type
Type
Type
Error control
(n/a)
4instanceofTypeLeft
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 referenceLeft
12^BitwiseLeft
13|BitwiseLeft
14&&LogicalLeft
15||LogicalLeft
16??Null coalescingRight
17?:TernaryLeft associative
(Non-associative in PHP 8.0.0)
18 =
+=
-=
*=
**=
/=
.=
%=
&=
|=
^=
<<=
>>=
??=
Assignment
Assignment
Assignment
Assignment
Assignment
Assignment
Assignment
Assignment
Assignment
Assignment
Assignment
Assignment
Assignment
Assignment
Right
19yield fromYield from(n/a)
20yieldYield(n/a)
21printPrint(n/a)
22andLogicalLeft
23xorLogicalLeft
24orLogicalLeft

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.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

READ MORE FROM THIS SERIES:



Leave a Comment