PHP Operators


title: PHP Operators

PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

OperatorNameExampleResult
+Addition$a + $bSum of $a and $b
Subtraction$a – $bDifference of $a and $b
*Multiplication$a * $bProduct of $a and $b
\/Division$a / $bQuotient of $a and $b
\%Modulus$a % $bRemainder of $a divided by $b
**Exponentiation$a ** $bResult of raising $a to the $b’th power

PHP Comparison Operators

The PHP comparison operators are used to compare two values

OperatorNameExampleResult
==Equal$a == $bReturns true if $a is equal to $b
===Identical$a === $bReturns true if $a is equal to $b, and they are the same type
!=Not equal$a != $bReturns true if $a is not equal to $b
<>Not equal$a <> $bReturns true if $a is not equal to $b
!==Not identical$a !== $bReturns true if $a is not equal to $b, or they are different types
>Greater than$a > $bReturns true if $a is greater than $b
\<Less than$a < $bReturns true if $a is less than $b
>=Greater than or equal to$a >= $bReturns true if $a is greater than or equal to $b
\<=Less than or equal to$a <= $bReturns true if $a is less than or equal to $b
\<=>Spaceship$a <=> $bReturns an integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b.

Note: Spaceship operator is available as of PHP 7+

Spaceship example

echo 1 <=> 0; // returns 1 echo 1 <=> 1; // returns 0 echo 1 <=> 2; // returns -1

More Information:

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.