Software Crafter

PHP Equality and Identity Operators

I am working on learning the Zend Framework, and along with that PHP as well (of course). I stumbled upon a code snippet on the internet which I didn't realize would work. I have seen the use of the identify operator in PHP before but I soon realized I didn't know all that I should. So I dived into this topic a little bit more and soon found out that more seasoned PHP programmers aren't too fluent in the differences between the two operators and what is (and not) possible.

Most programmers are accustomed to the equality operator as noted by '=='. This operator performs a check on the operands and if they are equal (you will soon learn equal has a very broad definition) then it will return true. What I mean by a broad definition is that the PHP interpreter will perform a conversion based upon what the operand contains. In an attempt to keep my examples more simple I will be talking about numerics and strings.

If both operands are a string value, but both contain a numeric expression the interpreter will attempt to convert both expressions to a numeric value then perform the numeric comparison. If one of the operands is a numeric value and you compare it to a string type, the string is converted to a numeric value as well. If the string conversion fails, then a 0 is returned. As an example, if you run the following code, it will print out β€˜1’.

<?php
  // This prints 1
  print ("000" == "0" && "0" == 0 && 0 == "cat");
?>

The following will result in a string comparison instead of a numeric comparison. The PHP interpreter recognizes β€œ0” as a numeric expression, but since β€œcat” is not a numeric expression then they are both left alone and compared as strings.

<?php
  // Does nothing
  print ("0" == "cat");
?>

If you are more comfortable with a strongly typed language such as Java or C++ then the identity operator is going to be what you would be more accustomed to. In PHP this is denoted as '===', this performs tests as you are more familiar with. The identity operator will return true if both operands are equal and of the same type. The above two examples, if used with the identity operator would return false in all cases.

In the following example since $a is not of a valid numeric expression no conversion takes place and a string comparison is performed. Conversely $c is a valid numeric expression (see Scientific Notation) this is converted and evaluated.

<?php
  $a = "0f0"; // This is not hex, as it does not start with 0x, ex. "0xf0"
  $b = "000";

  // This fails
  print ($a == $b); // "0f0" == "000"

  $c = "0e0";

  // This is successful
  print ($c == $b); // "0e0" == "000"
?>

Just one more last example, here both variables are valid numeric expressions. They both evaluate to 0 using the scientific notation.

<?php
  $a = "0e2";
  $b = "0e3";

  // This is successful
  print ($a == $b);
?>

I hope this has shed some light on the subject. The following are some references which helped me understand this a little better.

BlueShoes PHP Syntax Exam

PHP Docs for Comparison Operators - And a comment which started me on this journey