Knowpapa.com - a developer's blog

Difference between ‘=’ (assignment) ‘==’ (equal) and ‘===’ (identical) comparison operators in PHP

“=”(assignment)

The “=” sign in PHP is used for assignment of values ($var = “foo”) or for plainĀ  arithmetical operations ($a = $b + $c).

‘==’ (equal)

The ‘==’ (equal) sign is always used to check equivalence of values inĀ  a conditional operator. However ‘==’ only checks for value equivalence and does not check if they are of the same or different type. For example the following code will return Y – (same value but different type).

 if("55" == 55) return "Y";
 else           return "N";

Use of ‘=’ instead of ‘==’ is one of the most common mistakes by beginner PHP programmers.

‘===’ (equal)

The ‘===’ (equal) sign checks equivalence of both value & type. Thus the following code will return N. (same value but different types – string & integer).

 if("55" === 55) return "Y";
 else           return "N";

Modifying the above code to:

 if("55" === (string)55) return "Y";
 else           return "N";

will now return Y as now both the type and value matches.