Php equal (==) vs identical (===)

Equal (==) compares two values after type changing one value to another. Identical compare values and types both and returns true only if they both are same. Some php examples to cover frequently covering cases:

Compare “0” and 0 using == and ===

<?php
  var_dump("0" == 0);
  var_dump("0" === 0);
?>
bool(true)
bool(false)
Env: PHP 5.5.9 (Linux)

Compare “” and 0 using == and ===

<?php
  var_dump("" == 0);
  var_dump("" === 0);
?>
bool(true)
bool(false)
Env: PHP 5.5.9 (Linux)

Compare “0” and “” using == and ===

<?php
  var_dump("0" == "");
  var_dump("0" === 0);
?>
bool(false)
bool(false)
Env: PHP 5.5.9 (Linux)

Note that “0” and “” do not match even when using ==

What matches with FALSE using ==

<?php
  var_dump(FALSE == FALSE);
  var_dump(NULL == FALSE);
  var_dump(0 == FALSE);
  var_dump("0" == FALSE);
  var_dump("" == FALSE);
  var_dump(array() == FALSE);
?>
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Env: PHP 5.5.9 (Linux)

What matches with NULL using ==

<?php
  var_dump(FALSE == NULL);
  var_dump(NULL == NULL);
  var_dump(0 == NULL);
  var_dump("0" == NULL);
  var_dump("" == NULL);
  var_dump(array() == NULL);
?>
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
Env: PHP 5.5.9 (Linux)

Note that “0” does not match with NULL

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments