开发者

Difference between value = ""; and value = NULL; in PHP?

开发者 https://www.devze.com 2022-12-16 17:34 出处:网络
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

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 e开发者_如何转开发mpty string response from mysql database instead of empty array response.

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


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.


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'


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号