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?
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(), Nullare 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().
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
NULLwith no quotes.
So if you want an EMPTY field, set it to ""
But if you want a NULL field, set it to NULL
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).