PHP Operators

In this tutorial, you will learn how to perform certain activities on variables and values using operators in PHP.


PHP operators are symbols that perform certain operations on operands. For example, subtraction (-) symbol tells PHP to subtract two variables or values, while less than (<) symbol is an operator that tells PHP to compare two values.

Operators in PHP can be categorized as below.

PHP Arithmetic Operators

Arithmetic operators are used to perform different kinds of arithmetic operations like addition, subtraction, multiplication, etc. Below is the list of all arithmetic operators.

Operator Name Example Output
+ Addition $a + $b Sum of $a and $b
Subtraction $a – $b Difference of $a and $b
* Multiplication $a * $b Product of $a and $b
/ Division $a / $b Quotient of $a and $b
% Modulus $a % $b Remainder of $a divided by $b
** Exponentiation $a ** $b $a raised to the power $b

PHP Assignment Operators

Assignment operators are used to assign values to variables in PHP. The basic assignment operator is '='.

Operator Name Example Explanation
= Assign $a = $b The value of $b is assigned to $a.
+= Add then Assign $a += $b Addition same as $a = $a + $b
-= Subtract then Assign $a -= $b Subtraction same as $a = $a – $b
*= Multiply then Assign $a *= $b Multiplication same as $a = $a * $b
/= Divide then Assign (quotient) $a /= $b Find quotient same as $a = $a / $b
%= Divide then Assign (remainder) $a %= $b Find remainder same as $a = $a % $b

PHP Comparison Operators

Comparison operator in PHP is used to compare two variables or values.

Operator Name Example Explanation
== Equal $x == $y True if $x is equal to $y
=== Identical $x === $y True if $x is equal to $y, and they are of the same type
!= Not equal $x != $y True if $x is not equal to $y
<> Not equal $x <> $y True if $x is not equal to $y
!== Not identical $x !== $y True if $x is not equal to $y, or they are not of the same type
< Less than $x < $y True if $x is less than $y
> Greater than $x > $y True if $x is greater than $y
>= Greater than or equal to $x >= $y True if $x is greater than or equal to $y
<= Less than or equal to $x <= $y True if $x is less than or equal to $y

PHP Incrementing and Decrementing Operators

This operators are used to increment and decrements variables value.

Operator Name Explanation
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
–$x Pre-decrement Decrements $x by one, then returns $x
$x– Post-decrement Returns $x, then decrements $x by one

PHP Logical Operators

The logical operators are used to work in combination with conditional operators.

Operator Name Example Explanation
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $$x or $y is true
! Not !$x True if $x is not true

PHP String Operators

There are two string operators available in PHP which works on the string value.

Operator Name Example Explanation
. Concatenation $x . $y Concatenate both $x and $y
.= Concatenation and Assignment $x .= $y First concatenate $x and $y, then assign the concatenated string to $x, e.g. $x = $x . $y

PHP Array Operators

The array operators are used to compare the values of an array.

Operator Name Example Explanation
+ Union $x + $y Union of $x and $y
== Equality $x == $y Return TRUE if $x and $y have the same key/value pair
!= Inequality $x != $y Return TRUE if $x is not equal to $y
=== Identity $x === $y Return TRUE if $x and $y have the same key/value pair of the same type in the same order
!== Non-Identity $x !== $y Return TRUE if $x is not identical to $y
<> Inequality $x <> $y Return TRUE if $x is not equal to $y

PHP Operators Precedence

Below are the operators precedence in PHP along with associativity.

Operators Associativity Additional Information
clone new (n/a) clone and new
** right arithmetic
++ — ~ (int) (float) (string) (array) (object) (bool) @ (n/a) types and increment/decrement
instanceof left types
! (n/a) logical
* / % left arithmetic
+ – . left arithmetic and string
<< >> left bitwise
< <= > >= non-associative comparison
== != === !== <> <=> non-associative comparison
& left bitwise and references
^ left bitwise
| left bitwise
&& left logical
|| left logical
?? right null coalescing
? : left ternary
= += -= *= **= /= .= %= &= |= ^= <<= >>= ??= right assignment
yield from (n/a) yield from
yield (n/a) yield
print (n/a) print
and left logical
xor left logical
or left logical

Please get connected & share!

Advertisement