• Tutorial List

PHP Comparison Operators & Examples with if Statement

Last updated May 15, 2020
PHP Comparison Operators

PHP Comparison Operators

The PHP Comparison Operators are used to compare two values and it always returns a boolean value (True or False).


Table of the comparison operators

OperatorNameExampleResult
==Equal
$x == $y
Returns true if $x is equal to $y
===Identical
$x === $y
Returns true if $x is equal to $y, and they are of the same data type
!=Not Equal
$x != $y
Returns true if $x is not equal to $y
<>Not Equal
$x <> $y
Returns true if $x is not equal to $y
!==Not Identical
$x !== $y
Returns true if $x is not equal to $y, or they are not of the same data type
<Less than
$x < $y
Returns true if $x is less than $y
>Greater than
$x < $y
Returns true if $x is greater than $y
>=Greater than or equal to
$x >= $y
Returns true if $x is greater than or equal to $y
<=Less than or equal to
$x <= $y
Returns true if $x is less than or equal to $y
PHP comparison operators

Comparison Operators Examples with if Statement

== Equal Example:

The Equal operator(==) returns true when two values are equal. Otherwise, it will return false.

<?php
  if(2 == 2){
    echo "1 - Condition is true<br>";
  }

  if('google' == 'google'){
    echo "2 - Condition is true<br>";
  }

  if('google' == 5){
    echo "3 - Condition is true<br>";
  }

  if('google' == 'yahoo'){
    echo "5 - Condition is true<br>";
  }

  // with variable
  $name = "John";

  if($name == 'John'){
    echo "Yes Name is John";
  }
?>
Browser Output
1 – Condition is true
2 – Condition is true
Yes Name is John

=== Identical Example:

The Identical operator(===) returns true when two values are equal, and their data types are the same. Otherwise, it will return false.

<?php
  /* 
  == Operator does not check data types of values or variables
  for that it will return true
  */
  if(2 == '2'){
    echo "1 - Condition is true<br>";
  }

  /*
  First number data type is INTEGER 
  but the Second number data type is STRING
  */
  if(2 === '2'){
    echo "2 - Condition is true<br>";
  }
  else{
    echo "2 - Condition is fase<br>";
  }

  // both numbers data types are integer
  if(2 === 2){
    echo "3 - Condition is true";
  }
  else{
    echo "3 - Condition is fase";
  }
?>
Browser Output
1 – Condition is true
2 – Condition is fase
3 – Condition is true

!= , <> Not Equal Example:

The Not Equal operator(!= and <>) returns true when two values are not equal. Otherwise, it will return false.

<?php
  $name = "John";

  // condition is true, because both names are not equal
  if($name != 'Adam'){
    echo "\$name is not equal to Adam<br>";
  }
  else{
    echo "\$name is equal to Adam<br>";
  }

  // condition is false, because both names are equal
  if($name != 'John'){
    echo "\$name is not equal to John<br>";
  }
  else{
    echo "\$name is equal to John<br>";
  }

  if(2 != 5){
    echo "2 is not equal to 5<br>";
  }

  // you can use also <> operator to perform not equal condition
  // Both work the same
  echo "<hr><strong><> - Operator</strong><br>";

  if(2 <> 5){
    echo "2 is not equal to 5";
  }
?>
Browser Output
$name is not equal to Adam
$name is equal to John
2 is not equal to 5
<> – Operator
2 is not equal to 5

!== Not Identical Example:

The Not Identical Operator(!==) returns true when two values are not equal or they are not of the same data type. Otherwise, it will return false.

<?php
  // != operator dose not check data types of values or variables
  if(5 != '5'){
    echo "1 - Condition is true<br>";
  }
  else{
    echo "1 - Condition is false<br>";
  }

  /*
  it will return true,
  because 5 integer is not equal to 5 string
  */
  if(5 !== '5'){
    echo "2 - Condition is true<br>";
  }
  else{
    echo "2 - Condition is false<br>";
  }
?>
Browser Output
1 – Condition is false
2 – Condition is true

< Less Than Example:

The Less Than Operator(<) returns true when the first value is less than the second value. Otherwise, it will return false.

<?php
// TRUE EXAMPLE
if(4 < 5){
    echo "True";
}
else{
    echo "False";
}

// LINE BREAKER
echo "<br>";

// FALSE EXAMPLE
if(5 < 4){
    echo "True";
}
else{
    echo "False";
}
?>
Browser Output
True
False

> Greater Than Example:

The Greater Than Operator(>) returns true when the first value is Greater than the second value. Otherwise, it will return false.

<?php
  // FALSE EXAMPLE
  if(4 > 5){
      echo "True";
  }
  else{
      echo "False";
  }

  // LINE BREAKER
  echo "<br>";

  // TRUE EXAMPLE
  if(5 > 4){
      echo "True";
  }
  else{
      echo "False";
  }
?>
Browser Output
False
True

>= Greater than or Equal Example:

The Greater Than or Equal Operator(>=) returns true when the first value is greater than or equal to the second value. Otherwise, it will return false.

<?php
  // TRUE EXAMPLE
  if(10 >= 5){
      echo "True";
  }
  else{
      echo "False";
  }

  echo "<br>";
  // TRUE EXAMPLE
  if(5 >= 5){
      echo "True";
  }
  else{
      echo "False";
  }

  echo "<hr>";

  // FALSE EXAMPLE
  if(5 >= 10){
      echo "True";
  }
  else{
      echo "False";
  }
?>
Browser Output
True
True
False

<= Less Than or Equal Example:

The Less Than or Equal Operator(<=) returns true when the first value is Less than or equal to the second value. Otherwise, it will return false.

<?php
  // TRUE EXAMPLE
  if(6 <= 15){
      echo "True";
  }
  else{
      echo "False";
  }

  echo "<br>";

  // TRUE EXAMPLE
  if(8 <= 8){
      echo "True";
  }
  else{
      echo "False";
  }

  echo "<hr>";

  // FALSE EXAMPLE
  if(10 <= 6){
      echo "True";
  }
  else{
      echo "False";
  }
?>
Browser Output
True
True
False