Possible Duplicates:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?
Reference - What does this symbol mean in PHP?
php not equal to != and !==
What are the !==
and ===
operators in this code snippet?
if ( $a !== null ) // do something
if ( $b === $a ) // do something
== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: operand1 == operand2. === Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.
” and “==!” in PHP. !== Operator: It is called as non-identical operator. It returns true if operands are not equal, or they are not of the same type.
The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.
They are identity equivalence operators.
1 == 1
1 == "1"
1 === 1
1 !== "1"
true === true
true !== "true"
true == "true"
All of these equate to true. Also check this link provided by @mbeckish
They are strict type comparison operators. They not only check the value but also the type.
Consider a situation when you compare numbers or strings:
if (4 === 4) // same value and type
{
// true
}
but
if (4 == "4") // same value and different type but == used
{
// true
}
and
if (4 === "4") // same value but different type
{
// false
}
This applies to objects as well as arrays.
So in above cases, you have to make sensible choice whether to use ==
or ===
It is good idea to use ===
when you are sure about the type as well
More Info:
=== also checks for the type of the variable.
For instance, "1" == 1
returns true but "1" === 1
returns false. It's particularly useful for fonctions that may return 0 or False (strpos for instance).
This wouldn't work correctly because strpos returns 0 and 0 == false
if (strpos('hello', 'hello world!'))
This, however, would work :
if (strpos('hello', 'hello world!') !== false)
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