Comparison operators

Comparison operators are generally used inside a construct such as an if statement in which you need to compare two items. For example, you may wish to know whether a variable you have been incrementing has reached a specific value, or whether another variable is less than a set value, and so on.

Operator   Description                 Example
==         Is equal to                 $j == 4
!=         Is not equal to             $j != 21
<          Is less than                $j < 100
>          Is greater than             $j > 3
>=         Is greater than or equal to $j >= 15
<=         Is less than or equal to    $j <= 8
<>         Is not equal to to          $j <> 23
===        Is identical to to          $j === "987"
!==        Is not identical to to      $j !== "1.2e3"

Note the difference between = and ==. The first is an assignment operator, and the second is a comparison operator. Even advanced programmers can sometimes mix up the two when coding hurriedly, so be careful. Remember, make just one bug per day 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *