What is the difference between equal and identical comparison operators in PHP?

Possible duplicate:
How do operators compare equality (== double equals) and identity (=== triple equals)?

I know the main difference between == and === , but can some experienced coders tell me some practical examples for both cases?

+3
source share
3 answers

== checks if two operands are equal or not. === checks the values, as well as the type of two operands.

 if("1" == 1) echo "true"; else echo "false"; 

The above will print true .

 if("1" === 1) echo "true"; else echo "false"; 

The above will output false .

 if("1" === (string)1) echo "true"; else echo "false"; 

The above will print true .

+17
source

The easiest way to show this is with strings. Two examples:

 echo ("007" === "7" ? "EQUAL!" : "not equal"); echo ("007" == "7" ? "EQUAL!" : "not equal"); 
+1
source

In addition to @DavidT. For example, a more practical example is the following:

 $foo = "Goo"; $bar = "Good Morning"; if (strpos($bar,$foo)) echo "Won't be seen, returns false because the result is in fact 0"; if (strpos($bar,$foo) !== false) echo "True, though 0 is returned it IS NOT false)"; 
+1
source

All Articles