0

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
  • 4
    Is this question about PHP, or about SQL databases? If it is about PHP, how does the database remark factor in? Commented Jan 12, 2010 at 16:58

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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).
0

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'

Comments

0

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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.