# Comparison Operators in PHP

Comparison operators are used to create expressions that evaluates to either TRUE or FALSE when used in a conditional test. Lets go through each of those operators to understand how they work.

## Equals Operator (==)

Two equal signs (==) collectively known as the ‘equals’ operator. It checks for the equality of its two operands. If both operands are equal in their values, the expression evaluates to TRUE. If two operand values are different from each other, then the expression evaluates to FALSE.

Consider below two variables:

<?php$myname = 'steve';$yourname = 'bill';

Here, if we say **$myname == $yourname** , it is a FALSE statement. Thats because as you can see **$myname**’s value is not the same as **$yourname**’s value.

Lets take another couple of variables:

<?php$myage = 10;$yourage = 10;

This time if we say **$myage == $yourage**, it is a TRUE statement. Because both **$myage** and **$yourage** has 10 as their values.

## Not Equals Operator (!=)

The exclamation mark followed by an equal sign (!=) collectively identified as ‘**not equals**’ operator. It checks for the difference of its two operands. If operands are different in their values, the expression evaluates to TRUE. That means ‘Yes! there is a difference’. If operand values are similar then the expression evaluates to FALSE. Meaning ‘No! there is no difference’. This is the exact opposite behavior of the equals operator we’ve discussed above.

Lets put **$myname** and **$yourname** from the previous example into an expression using != operator. If we write **$myname != $yourname** , it will evaluate as a TRUE statement because as you can see both have two different values. However if we put **$myage != $yourage** , that is a FALSE statement because both variables have the similar values.

## Less Than Operator (<)

< is called the ‘less than’ operator. It checks to see if its left operand’s value is less than its right operand’s value. If the left operand value is less than the right operand value, the expression evaluates to TRUE. Otherwise in the opposite case, the expression evaluates to FALSE.

Consider below two variables:

$my_number = 10;$your_number = 20;

If we say, **$my_number < $your_number** , it is a TRUE statement because as you can see **$my_number**’s value 10 is in fact less than **$your_number**’s value 20. If we swap those two operands in opposite direction and write it as, **$your_number** < **$my_number**, it will be a FALSE statement because **$your_number**’ value(20) is actually higher than the **$my_number**’ value 10.

## Greater Than Operator (>)

> is called the greater than operator. It is used to check if its left operand’s value is higher than its right operand’s value. If the left operand value is higher than the right operand value, the expression evaluates to TRUE. Otherwise the expression evaluates to FALSE. This is exactly the opposite behavior of less than operator we’ve discussed above.

Let’s test above **$my_number** against **$your_number** along with the Greater than operator:

If we say **$my_number > $your_number**, that will evaluate to FALSE because the left operand, **$my_number**’s value (10) is in fact lesser than the right operand, **$your_number**’s value(20).

If we swap the variables to say **$your_number > $my_number**, that will evaluate to TRUE because this time the left operand **$your_number**’s value(20) is in fact higher than the right operand, **$my_number**’s value (10).

## Less than or Equal to Operator (<=)

When the less than sign/operator combined with an equal sign it is identified as Less than or Equal to operator. Less than or Equal to operator checks to see if its left operand’s value is less than or equal to its right operand’s value. As you can see, this operator looks for two conditions. If the left is less than the right or if the left equals to the right. When either one of these two conditions are satisfied, the expression evaluates to TRUE. If both conditions fails the expression evaluates to FALSE. Lets look at an example use case:

Create a new variable and assign it a value:

`$mynumber = 10;`

Now consider below three scenarios which use **$mynumber** along with Less than or Equal to operator:

- If we say,
**$mynumber <= 10**, that is a TRUE statement because**$mynumber**is in fact has a value which equals to right operand’s value (10). You can see its not less than the right but still equals. So one out of two conditions are satisfied. - If we say,
**$mynumber <= 15**, that is also a TRUE statement because this time**$mynumber**’s value (10) is of course less than its right operand’s value (15). Again, you can see one out of two conditions are satisfied. - If we say,
**$mynumber <= 5**, that will be a FALSE statement because**$mynumber**’s value (10) is not less than nor equals to its right (5). Neither of two conditions have met.

## Greater than or Equal to Operator (>=)

Greater than or Equal to operator checks to see if its left operand’s value is higher than or equal to the value of its right operand. Just as we saw with the previous operator, this operator also looks for two conditions. If the left is higher than the right or if the left equals to the right. When either one of these two conditions are satisfied, the expression evaluates to TRUE. If both conditions fails the expression evaluates to FALSE. Lets see an example:

Create a new variable and assign it a value:

`$currentyear = 2017;`

Now consider below three scenarios which use **$currentyear** along with Greater than or Equal to operator:

- If we say,
**$currentyear >= 2010**, that is a TRUE statement because**$currentyear**is in fact has a value which is greater than its right operand’s value (2010). You can see its not equals to the right but greater. So one out of two conditions are satisfied. - If we say,
**$currentyear >= 2017**, that is also a TRUE statement because this time**$currentyear**’s value (2017) is equals to its right operand’s value (which is also 2017). But its not greater than the right. Again, you can see one out of two conditions are met. - If we say,
**$currentyear >= 2020**, that will be a FALSE statement because**$currentyear**’s value (2017) is not less than nor equals to its right’s (2020). This time neither of two conditions have met.