tags:

views:

71

answers:

3

What is the difference between value = NULL; and value = ""; in PHP ?

Am facing wierd response as if I set value = ""; than I get empty array response from database which is what am supposed to get but if I set value = NULL; than I get empty string response from mysql database instead of empty array response.

I am not sure why this is the case. Any Suggestions !!!

+1  A: 

Have a look at

Working with NULL Values

The NULL value can be surprising until you get used to it. Conceptually, NULL means “a missing unknown value” and it is treated somewhat differently from other values. To test for NULL, you cannot use the arithmetic comparison operators such as =, <, or <>. Use the IS NULL and IS NOT NULL operators instead.

astander
thats not php??
antpaw
No, but it is MySQL, which is also tagged in the question, with no specification of which the question applies to. Up-voted to reduce the karma-hit (and because it's a good answer, obviously).
David Thomas
A: 

Value = NULL sets the variable to nothing, basically. Whereas Value = "" sets it to a blank string and while a blank string might look like nothing, it technically is a value of sorts. NULL and "" are both different values.

Parrots
They actually are quite different http://php.net/manual/en/language.types.null.php :$var = "";is_null($var) is false.
Parrots
Ok, your right:<?phpif ('' == null) echo " '' == null ";if ('' === null) echo " '' === null ";Outputs:'' == null
Mr-sk
A: 

empty strings and NULL are treated differently in MySQL. I had a similar question about this issue:

why use NOT NULL default = ''?

You have 2 solutions here:

  • Set the database field(s) you are checking to NOT NULL default '' or something similar.
  • rewrite your queries so that check both cases. ex: 'where field = '' OR field IS NULL'
GSto