October 31st, 2008Triple Equal Operator and NULL in PHP
A PHP variable has a value and a type. In most practice cases we consider only about the value of the variable. But there may be times we have to consider the type of the variable as well.
For an example value NULL (or 0 or whatever of following you likeĀ to call it) can be assigned to a variable with different types like this.
| $x = NULL; | NULL data type. Yea it is the real NULL. |
| $x = 0; | Integer data type. |
| $x = 0.0; | Float data type. |
| $x = FALSE; | Boolean data type. |
| $x = “”; | String data type. |
You can check the operator ‘===’ (triple equal operator) to check equality of both their values and types simultaneously.
You can have a good idea about this by looking at the following piece of code.
// lets check the equality of NULL and 0 with // double equal operator if(NULL == 0) { echo "NULL == 0 is TRUE</br>"; } else { echo "NULL == 0 is FALSE</br>"; } // now lets check the equality of NULL and 0 with // triple equal operator if(NULL === 0) { echo "NULL === 0 is TRUE</br>"; } else { echo "NULL === 0 is FALSE</br>"; }
This will eventually print the following result.
NULL == 0 is TRUE NULL === 0 is FALSE
PHP variables including class variables get the NULL value and NULL type by default. So if no one assign them a value it remains NULL.
So in a case you have to check for unused variables (or for “NULL”-ness of a variable) you should always use the triple equal operator (===) with NULL token. Otherwise you may mistakenly treat not NULL things like ‘0′ or empty string (”") as null that may have been valid data for your application.
![[Ask]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/ask.png)
![[Bloglines]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/bloglines.png)
![[del.icio.us]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/digg.png)
![[diigo]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/diigo.png)
![[dzone]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/google.png)
![[MySpace]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/myspace.png)
![[MyWeb]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/myweb.png)
![[Newsvine]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/newsvine.png)
![[PlugIM]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/plugim.png)
![[Reddit]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/reddit.png)
![[Slashdot]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/slashdot.png)
![[Spurl]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/spurl.png)
![[StumbleUpon]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Twitter]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/email.png)