NYCPHP Meetup

NYPHP.org

[nycphp-talk] Need help understanding NULL

lists at nopersonal.info lists at nopersonal.info
Sat Aug 29 20:21:43 EDT 2009


David, thanks for the detailed explanation & examples.

David Krings wrote:
> 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.

Okay, wait--but what about what Dan said re NULL values not being added
to averages, making them useful statistically? In that case wouldn't you
want NULL to stay NULL? IOW, in his case the query would would only
retrieve values WHERE foo != NULL? But then the manual says that the
arithmetic comparison operators won't work with NULL, so that can't be
right...

Hang on, let me think a minute... Okay, so then I guess you'd just
assign a value of 0 or '' to anything that was NULL, and then have PHP
only calculate numbers where $foo >= 1? But if that's the case, then why
use NULL in the first place? That can't be right, so I must still be
missing some important point.

One last try: Maybe the query would be along the lines of SELECT * WHERE
foo NOT IN NULL, that way there wouldn't be any need to deal with it
because since it was never retrieved in the first place...?

Sorry for making you witness my somewhat scattershot thinking process,
but even the smallest sequences of programming logic can still be a big
challenge for me.

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

Got it.

> I hope I explained it in an understandable way.

You did an awesome job of explaining. Thanks again for taking the time
to help.




More information about the talk mailing list