In this tutorial, you shall learn about different categories of Operators in PHP, operators in each category, and how to use them in PHP programs, with dedicated tutorials to each operator.

PHP Operators

Operators perform operations on operands like variables or values.

In PHP, there are eight groups of operators based on the type of operations they perform. They are

  • PHP Arithmetic Operators
  • PHP Assignment Operators
  • PHP Comparison Operators
  • PHP Increment / Decrement Oeprators
  • PHP Logical Operators
  • PHP String Operators
  • PHP Array Operators
  • PHP Conditional Operators

Arithmetic Operators

PHP Arithmetic Operators perform mathematical arithmetic operations and return the result. Following are the arithmetic operators supported in PHP.

Arithmetic OperationOperator SymbolExampleDescription
Addition + $x + $yReturns sum of $x and $y.
Subtraction - $x - $yReturns difference of $y from $x.
Multiplication * $x * $yReturns product of $x and $y.
Division / $x / $yReturns the quotient when $x is divided by $y.
Modulus % $x % $yReturns reminder when $x is divided by $y.
Exponentiation ** $x ** $yReturns the result of $x raised to the power $y.

Tutorials

Assignment Operators

PHP Assignment Operators are used to optionally perform an action with given operands and assign the result back to given variable (left operand).

The following table specifies symbol, example, and description for each of the Assignment Operator in PHP.

Assignment OperationOperator SymbolExampleDescription
Simple Assignment=$x = 2Assign $x with 2.
Addition Assignment+=$x += 3Add 3 to the value of $x and assign the result to $x.
Subtraction Assignment-=$x -= 3Subtract 3 from $x and assign the result to $x.
Multiplication Assignment*=$x *= 3Multiply $x with 3 and assign the result to $x.
Division Assignment/=$x /= 3Divide $x with 3 and assign the quotient to $x.
Modulus Assignment%=$x %= 3Divide $x with 3 and assign the remainder to $x.

Tutorials

Comparison Operators

Comparison Operators are used to compare two operands if they are equal, greater, lesser, etc.

The following table gives more information about these Comparison Operators in PHP.

Relational OperationOperator SymbolExampleDescription
Equal==$x == $yReturns true if $x is equal to $y, else false.
Identical===$x === $yReturns true if $x is equal in value to $y, and if $x and $y are of the same datatype. Otherwise returns false.
Not equal!=$x != $yReturns true if $x is not equal to $y in value, else false.
Not equal<>$x <> $yReturns true if $x is not equal to $y in value, else false.
Not identical!==$x !== $yReturns true if $x is not equal in value to $y, or if $x and $y are of not the same datatype. Otherwise returns false.
Greater than>$x > $yReturns true if $x is greater than $y, else false.
Less than<$x < $yReturns true if $x is less than $y, else false.
Greater than or equal>=$x >= $yReturns true if $x is greater than or equal to $y, else false.
Less than or equal<=$x <= $yReturns true if $x is less than or equal to $y, else false.
Spaceship<=>$x <=> $yReturns an integer.
Less than zero if $x less than $y.
Equal to zero if $x is equal to $y.
Greater than zero if $x is greater than $y.

Tutorials

  • PHP Equal To
  • PHP Identical
  • PHP Not Equal
  • PHP Not Identical
  • PHP Greater Than
  • PHP Less Than
  • PHP Greater Than or Equal To
  • PHP Less Than or Equal To
  • PHP Spaceship

Increment / Decrement Operators

PHP Arithmetic Operators perform mathematical arithmetic operations and return the result. Following are the arithmetic operators supported in PHP.

Arithmetic OperationOperator SymbolExampleDescription
Pre-Increment ++
++$x
Increments the value of $x by one. The value of $x is updated during the execution of this statement itself.
Post-Increment ++ $x++Increments the value of $x by one. The value of $x is updated, only after the execution moves to next statement.
Pre-Decrement -- --$xDecrements the value of $x by one. The value of $x is updated during the execution of this statement itself.
Post-Decrement -- $x--Decrements the value of $x by one. The value of $x is updated, only after the execution moves to next statement.

Tutorials

  • PHP Pre-Increment
  • PHP Post-Increment
  • PHP Pre-Decrement
  • PHP Post-Decrement

Logical Operators

Logical Operators are used to create boolean conditions, modify a boolean expression, or combine two or more simple conditions to form a complex condition.

Java supports the following Logical Operators.

Logical OperationOperator SymbolExampleDescription
ANDand$x and $yReturns true if both $x and $y are true.
ORor$x or $yReturns true if at least one of $x, $y is true.
XORxor$x xor $yReturns true if $x and $y are different boolean values.
AND&&$x and $yReturns true if both $x and $y are true.
OR||$x or $yReturns true if at least one of $x, $y is true.
NOT!!xReturns true if $x is false, and vice versa.

Tutorials

  • PHP AND
  • PHP OR
  • PHP NOT

String Operators

There are two string operators in PHP.

Operator NameOperator SymbolExampleDescription
Concatenation.$x . $yReturns the concatenation of $x and $y.
Concatenation Assignment.=$x .= $yConcatenates $x and $y, and assigns the result back to $x.

Tutorials

  • PHP String Concatenation
  • PHP String Concatenation Assignment

Array Operators

There are six array operators in PHP.

Relational OperationOperator SymbolExampleDescription
Union+$x + $yReturns an array created from the union of elements from $x and $y.
Equal==$x == $yReturns true if $x and $y have same values at every index.
Identical===$x === $yReturns true if $x and $y have same values and type at every index.
Not equal!=$x != $yReturns true if $x and $y are not equal.
Not equal<>$x <> $yReturns true if $x and $y are not equal.
Not identical!==$x !== $yReturns true if $x and $y are not equal in value and type.

Tutorials

  • PHP Array Union
  • PHP Array Equal
  • PHP Array Identical
  • PHP Array Not Equal
  • PHP Array Not Identical

Conditional Assignment Operators

There are two conditional assignment operators in PHP.

Operator NameOperator SymbolExampleDescription
Ternary?:condition? value1: value2Returns value1 if condition is true, else value2 if condition is false.
Null Coalescing??value1?? value2Returns value1 if value1 exists and not NULL, otherwise returns value2.

Tutorials

  • PHP Ternary
  • PHP Null Coalescing

Conclusion

In this PHP Tutorial, we learned about different operators in PHP, with exclusive tutorials for each of the operators.