NYCPHP Meetup

[nycphp-talk] Need help understanding NULL

lists at nopersonal.info lists at nopersonal.info
Sun Aug 30 11:12:30 EDT 2009


David Krings wrote:
> Dan mentioned a case where having NULL is of good use, because in his
> example it makes a difference if the person responding to a survey
> doesn't answer the question or answers it with 0. If there is no answer
> you cannot treat it the same way as 0. It is an example that I didn't
> think about.
> What I was talking about was in regards to variables in PHP being NULL.
> The "retrieve values WHERE foo != NULL" is SQL. That is a different
> language.

I need to be pay more attention to the differences between the way PHP &
MySQL handle things. Becuase they always seem to go hand in hand, it's
easy for me to forget that they're two separate languages.

> Just because I couldn't come up with a case for using NULL (and I tried
> to find one) doesn't mean there is one. Dan is right and his example is
> spot on. My explanation wasn't wrong, but incomplete.

Understood.

> I fired up my rusty PHP IDE and tried a few things. Turns out that SQL
> is way more picky about what NULL is, whereas PHP considers NULL often
> as 0 or an ampty string. For example...

<snip>

Wow, thanks for taking the time to do that. At some point between now &
next weekend I'm going to gather everyone's examples and play around
with them so I can see what happens for myself.

> So, what I propose you keep in mind is that PHP knows about NULL, but
> treats it as if it is 0 for numerical variables or as an empty string.
> In SQL it is not the case. If PHP's behaviour is right or wrong can be
> hotly debated. I like that it doesn't take it too serious with the
> variable type, but just to assume that some undefined variable is out of
> a sudden 0 is quite bold. But that's the way it is and when one knows
> about it one can deal with it.

I'll keep that in mind.

> See, I hate programming and programming hates me. I do like programming
> in PHP, because PHP isn't so anal about stuff like this as Java or C.
> That makes me think less. The problem is that you might end up with
> really bad results. For example when no submission is an error
> condition, but submitting an empty string or 0 is fine. In that case
> having NULL be the same as zero is not what you want. So while for most
> cases PHP's assumptions work out fine I wouldn't count on PHP being
> always right.

That's an important point, knowing that PHP can sometimes be a little
too forgiving.

> You are welcome, just keep in mind that PHP and SQL aren't the same,
> even when we often mush them together in code.

I have a feeling you guys aren't going to let em forget it. ;)

Bev







More information about the talk mailing list