tags:

views:

894

answers:

5

I used to set things like this when I wanted blank values.

$blankVar = '';

Then after some months, I decided this looked better and had a clearer intent.

$blankVar = null;

This worked without hiccup for a while, but recently with a PDO prepared statements I ran into a problem. Binding a value to null made the query fail, whilst binding it to '' did not. I needed to bind it to null, so that if a condition was met, it would insert blank data.

What are the differences between the 2? I still think equaling null (or at least a constant) looks better, so should I do this?

define('EMPTY', '');

Thank you

+6  A: 

null is a special placeholder value in the programming language that literally means "nothing". It's not 0, it's not an empty string, it's nothing. There is no value in memory being pointed to. An empty string, on the other hand, is still a string object, just a very short one :)

Rex M
Thanks Rex, this is the clearest answer (to me anyway).
alex
If it is the clearest answer, and I agree, why did you not accept it?
Chris Ballance
@Chris: Because the one I accepted gave me more information.. I voted this one up instead.
alex
+2  A: 

Well, it's one thing how nice each approach looks, but the main difference is that one is an empty string and the other is an uninitialized variable (null)

Alexandru Luchian
+1  A: 

Aside from the differences stated by Rex, there's two types of comparisons in PHP, loose and strict:

http://www.php.net/manual/en/types.comparisons.php

If strict comparisons or functions like is_null() are used in any capacity, you'll get different results. With loose comparisons, however, PHP is pretty lenient.

I don't know for sure, but you may be able to use your null approach, then just typecast when you're using the variable in the context where you had issues (i.e. pass (string) $blankVar). If that works, it may mean less changes are necessary to your code.

Dan Fego
This looks useful, so if I continue to set things to null, and if I have a hiccup, simply $var = (string)$myPossiblyNullString; ?
alex
PHP is weakly typed, so those sorts of things generally do work, but I haven't tried it myself.
Dan Fego
A: 

'null' is the unique thing that uninitialized variables refer to. You can set a variable to refer to null. There is also the 'unset' state meaning the variable doesn't exist at all. You can check that via the isset() function. Most often used to check if an element of array exists and is, for example, a way to see if $_GET['op'] has received a querystring param. You can also make a variable unset (remove an element of an array) via the unset() function. There is also a function empty() which will check if a variable is either NULL, FALSE, 0, or an empty string

Scott Evernden
+7  A: 

Null is just another datatype in PHP, which has only one value (null). Since PHP is a loosly typed language, it can be confusing how it handles different values.

"", 0, "0", False, array(), Null are all considered False in PHP.

Null, however, is a different kind of animal. The main incompatibility with using Null is that you cannot tell if it isset().

$x = false;
isset($x)  ->  true
echo $x    ->  ""

$y = null;
isset($y)  ->  false
echo $y    ->  ""

//$z is not set
isset($z)  ->  false
echo $z    ->  E_NOTICE

So null is odd in the sense that it doesn't follow normal variable rules in PHP (at least some). In most cases, it is fine.

When it comes to database columns, PHP's NULL has no place there. You see, SQL is a string based language. SQL's NULL must be represented by NULL with no quotes.

So if you want an EMPTY field, set it to ""

INSERT INTO foo SET bar = ""

But if you want a NULL field, set it to NULL

INSERT INTO foo SET bar = NULL

BIG DIFFERENCE.

But if you try to insert the PHP NULL directly, it will add zero characters to the query, (which leaves you with a blank or syntax error, depending on if you quoted it).

gahooa
Thanks for including your section on SQL.
alex