I know that !==
is used to compare variable types too, while !=
only compares values.
But I see that many people use !==
when they compare values, for example:
$stuff = 'foo';
if($stuff !== 'foo') // do...
Is there any reason they do this? Is !==
faster than !=
or what?
In this function, when it is compared the lenght of the array it is used != operator and when it is comparing all elements of the array it is using !==
The == and === operators are used to check the equality of two operands. The != and !== operators are used to check the inequality of two operands.
The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .
Inequality Operators: !=: Converts values if variables are different types before checking for inequality. !== : Checks both type and value for the two variables being compared.
It is wrong to say that !=
does only compare the value. Because it rather compares the types, applies a type conversion if necessary (see table of Comparison with Various Types) and then compares the (converted) values.
In contrast to that, !==
fails earlier if the types are not equal and no type conversion is done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With