I keep seeing variations of this:
Not equal
!=
Not equal, equal
!==
Which one is the standard or do they have different meanings?
I am guessing the latter also checks the value and the name if it's a string, while the former might just check the value only...
<> is exactly the same as != operator since both of them are parsed as T_IS_NOT_EQUAL token. And they have same precedence.
PHP strcmp() Function: The strcmp() is an inbuilt function in PHP that is used to compare two strings.
Identical Operator === The comparison operator called as the Identical operator is the triple equal sign “===”. This operator allows for a much stricter comparison between the given variables or values. This operator returns true if both variable contains same information and same data types otherwise return false.
Equality operator == converts the data type temporarily to see if its value is equal to the other operand, whereas === (the identity operator) doesn't need to do any type casting and thus less work is done, which makes it faster than ==.
==
and !=
check equality by value, and in PHP you can compare different types in which certain values are said to be equivalent.
For example, "" == 0
evaluates to true
, even though one is a string and the other an integer.
===
and !==
check the type as well as the value.
So, "" === 0
will evaluate to false
.
Edit: To add another example of how this "type-juggling" may catch you out, try this:
var_dump("123abc" == 123);
Gives bool(true)
!
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