NYCPHP Meetup

NYPHP.org

[nycphp-talk] Need help understanding NULL

David Krings ramons at gmx.net
Sat Aug 29 16:42:14 EDT 2009


lists at nopersonal.info wrote:
> I don't usually use NULL values for anything because no matter how much
> I read I can't figure out the purpose of it. So far this hasn't been a
> problem for me, but I figure it's going to trip me up sooner or later.
> 
> I've read the NULL sections of the PHP & MySQL manuals and even tried
> googling for articles, but the concept of a “a missing unknown value”
> makes my brain hurt. Can someone please, please, please attempt to
> explain--in the simplest terms possible--what the practical
> advantages/disadvantages of using it might be?


I struggled with it for some time as well, because for non-programmers 
nothing, empty, null, nada, and nichts are the same thing. I think the easiest 
way to get an idea is to look at how strings and integers are stored in a 
database table. Unless a default value is specified, the contents of a field 
in a database table are set to NULL. That NULL isn't the same as zero for an 
integer. Zero is a defined value of an integer variable the same way 1 or 
123456 would be. In case of strings, NULL is not the same as an empty string. 
So when you read records from a table and then use the field values to make 
comparisons you will find that NULL is unequal to zero or an empty string. 
While NULL is really nothing, 0 and "" are something - at least for a database 
and PHP, because then the database and PHP know exactly what is meant, whereas 
with NULL there is no defined value.
Same way the other way around. Let's say, you create a simple ledger 
application to balance your checkbook.  You have 100$ and spend it all on PHP 
books. So after entering the expense you end up with 0$, which is an explicit 
amount and the result of your calculation. NULL is not specific and if you do 
the math you get zero and not 'nothing'.
In regards to strings, let's say you have a string and you need to strip all 
vowels and all occurrences of "s". The original string is "sea", not you strip 
the characters out and you get "". Similar to the math task above, it is a 
definitive results, whereas NULL isn't.

That said, I think it is very important to initialize every variable you use 
in your script right at the beginning of the script, even if you never will 
use the variable with that value. The reason for that is that all your 
variables will have a definitive value. Same applies for variables that get 
values assigned that come from database tables or HTML forms (or better to 
say, any source no matter what). First order of business is to check it for 
being a NULL value and assigning it a definitive (explicit) value, which can 
include 0 or "". Otherwise PHP will complain or in case of integers just 
assume something. Typically, that is 0, but I rather tell PHP what it is than 
have PHP pull something out of its lower torso.

Sure, one could say that NULL is the same as 0 or "", but that is a purely 
arbitrary interpretation, although maybe a convenient one.

I hope I explained it in an understandable way.


David



More information about the talk mailing list