Difference Between == & ===

shivv89

== (Equal):

  • What it does: Checks if the values are the same, but it doesn’t care about the type of the values. It will convert types if needed to make the comparison.
  • Example:
$a = 5;
$b = '5';

if ($a == $b) {
    echo 'Equal';  // This will print because it converts the string '5' to the number 5
}

=== (Identical):

  • What it does: Checks if both the value and the type are exactly the same.
  • Example:
$a = 5;
$b = '5';

if ($a === $b) {
    echo 'Identical';
} else {
    echo 'Not identical';  // This will print because one is a number and the other is a string
}

In simple terms:

  • ==: Checks if the values are the same, no matter the type.
  • ===: Checks if both the value and the type are exactly the same.

Leave a Comment