NYCPHP Meetup

NYPHP.org

[nycphp-talk] Need help understanding NULL

Kristina D. H. Anderson ka at kacomputerconsulting.com
Sat Aug 29 16:47:09 EDT 2009


Bev,

The test for and behavior of NULL varies somewhat from language to 
language and database to database.

Let's take a basic example, you design a database table and you 
describe your LastName field as NOT NULL.

That means that every time you do an insert into the table, the field 
LastName MUST contain a value of some sort.  The purpose of that is 
basically to ensure that any query, unless it contains all of the 
required values, will error out if you attempt to do the insert without 
having a value in that field.

In most cases, not all cases, on INSERT, an empty string i.e. '' is not 
considered a NULL value, but an empty string which will result in ... 
well...an empty string being stored in your database, but the field 
will not show as NULL.  When you pull that out of the database, some 
languages will treat that as a NULL value and some will treat it as an 
empty string.

Or, in your database, you can specify NULL as the default value for the 
field, and therefore if you skip it in an insert, it will default to 
NULL.  The design of the database depends on the needs of the 
application.

As a test of how PHP treats NULL vs. empty strings with your database, 

Create values of NULL, ' ' (a string containing a space), '' (an empty 
string) and pull them out into variables and then you can begin to 
determine how, in your application, they are being treated.

In most cases, you should see NULLs and empty strings look very 
similar...but not always.  And the devil is definitely in the details, 
especially if your application is talking to other platforms that may 
handle these values differently.

It can help you to understand NULL, if you contemplate the difference 
between an empty string '' and NULL.  NULL is not a string because it's 
undetermined. But an empty string is a string that contains, well, 
nothing, but it is not NULL.

Kristina



> Matt Juszczak wrote:
> > something is NULL, then you don't know what it is.  It could be 
empty,
> > or it could be set.  You just don't know.
> 
> That's precise the part I can't seem to wrap my head around.
> 
> > If something is empty, then you know it's empty.  So if birthdate 
is set
> > to '', then you know they have no birthdate.  But if it's set to 
NULL,
> > you don't know if they have one or not.
> 
> Thanks for trying, Matt, but I still don't get the purpose of "knowing
> that you don't know" something, so to speak.
> 
> *headdesk*
> 
> Bev
> 
> 
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
> 
> http://www.nyphp.org/show_participation.php
> 
> 








More information about the talk mailing list