I'm learning validation stuff, and I just can't understand this:
if (strpos($value, "@") === false) { echo "Validation failed." }
What's the difference between ===
and ==
? and why can't we use ==
instead and also why is it === false
? does false means that @
is not in the $value
or it means 0
?
The Equality Operator ==
A == B
checks whether A and B are equal to each other, but not whether they are the same data type.
A pertinent example: 0 == false
is true
The Identity Operator ===
A === B
checks whether A and B are equal to each other also the same data type.
A pertinent example: 0 === false
is false
Application Here
Applying this to your case, if the @
was found as the first character of the string, strpos($value,"@")
would return 0
. If it is not found at all, it would return false
.
So to avoid confusing these two situations, the test must use ===
rather than ==
.
Useful references:
http://php.net/manual/en/function.strpos.php http://php.net/manual/en/language.operators.comparison.php
I've assumed this is php, but the equality and identity operators are common to many programming languages.