Wednesday 16 March 2016

Difference between == (equality or Double Equals) and === (identity or Triple Equals) in PHP

Difference between == (equality or Double Equals) and === (identity or Triple Equals) in PHP

== compares the values of variables for equality, type casting as necessary. 


=== checks and returns true only if the two variables are of the same type AND have the same value.


Examples:
a === a // returns true
a == a  // returns true

1 === "1" // returns false because 1 is an integer, "1" is a string
1 == "1"  // returns true because "1" gets casted to an integer, which is 1


"abc" == "abc" // returns true
"abc" === "abc" // returns true because both operands are strings and have the same value


For the security reasons, it is better to use identity operator whenever possible because it compares variable type also.

Enjoy your coding!!!

No comments:

Post a Comment