Skip to content
Related Articles

Related Articles

Difference between $a != $b and $a !== $b

Improve Article
Save Article
  • Last Updated : 20 Jul, 2022
Improve Article
Save Article

$a!=$b

This operator is also known as inequality operator. It is used to check the equality of both operands, however, the type of the operands does not match. For instance, an integer type variable is equivalent to the floating point number if the value of both the operands is same. It is a binary operator. 

PHP




<?php
 
// Assigning values to the
// variables a and b
$a = 5;
$b = 5.0;
 
if($a != $b) {
    echo("Both operands are not Equal");
}
else {
    echo("Both operands are Equal");
}
 
?>


Output

 Both operands are Equal

$a!==$b

This is non-identity operator. It is used to check the equality of both operands, however, the type of the operands also match. For instance, an integer type variable is not equivalent to the floating point number irrespective of the value of both the operands. It is a binary operator. 

PHP




<?php
 
// Assigning the values of
// variables a and b
$a = 5;
$b = 5.0;
 
if($a !== $b) {
    echo("Both operands are not same");
}
else {
    echo("Both operands are same");
}
 
?>


Output

Both operands are not same

Difference between != and !== operators:

!=

!==

It returns true if value of both operands are equal. It returns true if value and data type of both operands are equal.
It is known as Inequality Operator. It is known as Non-identity Operator.
Empty values are considered to be equivalent. Empty values should belong to same data type.

Example: The following code snippet illustrates the difference of both the operators in a simple way. 

PHP




<?php
   
// Assigning values to the
// variables a and b
$a = null;
$b = FALSE;
 
// != operator
if($a != $b) {
    echo("Both operands are not equal (Only Value)\n");
}
else {
    echo("Both operands are equal (Only Value)\n");
}
 
// !== operator
if($a !== $b) {
    echo("Both operands are not equal (Value and Data Types)");
}
else {
    echo("Both operands are equal (Value and Data Types)");
}
 
?>


Output

Both operands are equal (Only Value)
Both operands are not equal (Value and Data Types)

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!