Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between equal and identical comparison operators in PHP? [duplicate]

Tags:

php

Possible Duplicate:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?

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

like image 362
Vamsi Krishna B Avatar asked Jan 19 '11 07:01

Vamsi Krishna B


2 Answers

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

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

The above would output true.

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

The above would output false.

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

The above would output true.

like image 122
David Titarenco Avatar answered Oct 07 '22 23:10

David Titarenco


Easiest way to display it is with using strings. Two examples:

echo ("007" === "7" ? "EQUAL!" : "not equal"); 
echo ("007" == "7" ? "EQUAL!" : "not equal"); 
like image 28
Bjoern Avatar answered Oct 07 '22 23:10

Bjoern