Difference between $a != $b and $a !== $b
Last Updated :
20 Jul, 2022
$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
$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
$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
$a = null;
$b = FALSE;
if ( $a != $b ) {
echo ( "Both operands are not equal (Only Value)\n" );
}
else {
echo ( "Both operands are equal (Only Value)\n" );
}
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...