Operators

An operator is a symbol that takes one or more values to yield another value. There many different types of operators including Arithmetic Operators, Comparison Operators and Logical Operators.

Arithmetic Operators

Arithmetic operators are used to perform basic arithmetic, including addition, subtraction, multiplication and division.

Operator Name Example Result
+ 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 Divides $a by $b
% Modulo $a % $b Remainder of $a divided by $b

Comparison Operators

Comparison Operators are used to compare two values. When comparing values of different data types, for example a string and a number, PHP will attempt to convert the string to a number first and then do the comparison.

Operator Name Example Result
== Equal $a == $b TRUE if $a is equal to $b in value
=== Identical $a === $b TRUE if $a is equal to $b in value and data type
!= Not Equal $a != $b TRUE if $a is not equal to $b in value
!== Not Identical $a !== $b TRUE if $a is not equal to $b in value and data type
< Less than $a < $b TRUE if $a is less than $b
> Greater than $a > $b TRUE if $a is greater than $b
<= Less than or equal to $a <= $b TRUE if $a is less than or equal to $b
>= Greater than or equal to $a >= $b TRUE if $a is greater than equal to $b
<=> Spaceship $a <=> $b Returns 0 if $a is equal to $b

Returns 1 if $a is greater than $b

Returns -1 if $a is less than $b

Logical Operators

Logical Operator are used to combine and make more complex expressions.

Operator Name Example Result
! Not !$a TRUE if $a is not TRUE
&& And $a && $b TRUE if both $a and $b are TRUE.
|| Or $a || $b TRUE if either $a or $b is TRUE.

Caution

It is also possible to use and and or in place of && and ||. But these operators have different precedence and can result in unexpected results. Review the Logical Operators documentation for more information.