Last updated on September 4, 2023

The comparison operator is mainly used in PHP to compare one variable value with another. These values can be strings or numbers. In PHP, comparison operators take simple values (numbers or strings) as arguments. The comparison operator gives us the result in Boolean. Let’s try to understand with the help of the table.

Here are the comparison operators along with their syntax and operations in PHP.

Operator | Name | Syntax | Operation |

== | Equal to | $x == $y | Returns True if both the operands are equal |

!= | Not equal to | $x != $y | Returns True if both the operands are not equal |

<> | Not Equal To | $x <> $y | Returns True if both the operands are unequal |

=== | Identical | $x === $y | Returns True if both the operands are equal and are of the same type |

!== | Not identical | $x == $y | Returns True if both the operands are unequal and are of different types |

< | Less than | $x < $y | Returns True if $x is less than $y |

> | Greater than | $x > $y | Returns True if $x is greater than $y |

<= | Less than or equal to | $x <= $y | Returns True if $x is less than or equal to $y |

=> | Greater Than or Equal To | $x >= $y | Returns True if $x is greater than or equal to $y |

**Return values:** The comparison operators returns bool (true or false).

Let’s try to understand with the following examples below:

**1. Equality Operator (==)**

The equality operator returns true if both values are equal; otherwise, it returns false.

```
$x = 10;
$y = 10;
var_dump($x == $y);
// output: bool(true)
```

**2. Not equal to operator (!=)**

The not equal to (!=, <>) operator returns true if the left value is not equal to the righthand value; otherwise, it returns false.

```
$x = 20;
$y = 10;
var_dump($x != $y);
// output: bool(true)
```

**3. Identical operator (===)**

The identical operator returns true if both values are equal and have the same type; otherwise returns false.

```
$x = "20";
$y = 20;
var_dump($x === $y);
//Output: bool(false)
```

**4. Not identical operator (!==)**

The not identical operator (!==) returns true if the values are not equal or they do not have the same type; otherwise, it returns false.

```
$x = 20;
$y = 10;
var_dump($x != $y);
//Output: bool(true)
$x = 20;
$y = "20";
var_dump($x != $y);
//Output: bool(false)
```

**5. Greater than (>)**

The greater than returns true if the left value is greater than the righthand value; otherwise, it returns false.

```
$x = 10;
$y = 20;
var_dump($x > $y);
// output: bool(false)
```

**6. Greater than or equal to (>=)**

The greater than or equal to operator returns true if the left value is greater than or equal to the righthand value; otherwise, it returns false.

```
$x = 20;
$y = 10;
var_dump($x >= $y);
// output: bool(true)
```

**7. Less than (<)**

The less-than operator returns true if the left value is less than the righthand value; otherwise, it returns false.

```
$x = 20;
$y = 10;
var_dump($x < $y);
// output: bool(false)
```

**8. Less than or equal to (<=)**

If the left value is less than or equal to the righthand value, the less than or equal to the operator returns true; otherwise, it returns false.

```
$x = 20;
$y = 20;
var_dump($x <= $y);
// output: bool(true)
```

**Conclusion:**

This article described Comparison Operators with examples. Also, we explained that how to use the PHP comparison operators to compare two values of the same or different types.

Share on:

Related Posts

Published on 03 October

Published on 25 September

Published on 21 September

Published on 19 September

Published on 18 September