Web Design Forums

Welcome! Please register or log in: Forgot your password? Why register?
You are here: Web Design Forums » Programming Help » PHP » NULL or "" ? RSS

NULL or "" ?

This thread was started by medlington and has been viewed 363 times, and contains 5 replies, with the last reply made by medlington.
Post Reply
1
36 points at 100%
medlington, WDF Addict! Home page   Private message  
Posted February 5 '10 at 06:19 AM
      Posts: 160
Hi,

I've learned PHP myself and as such have my own way of doing things.

When Ive been declaring a variable as nothing I've just been doing something like this:

$variable ""

Recently I came across the NULL and have realised I could declare the variable like this:

$variable NULL

Whats the difference, will I get problems doing it one way over the other?

Advertisement Register for free to hide these ads and participate in discussions!

2
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted February 5 '10 at 08:39 AM
      Posts: 3,100
This explains it better than I can:
http://www.devshed.com/c/a/MySQL/Nul...Empty-Strings/

I never use NULL ...
I do it the way you have done it ... $variable = "";

To me, it just seems easiest, and I always know which way it is.
But there is probably a valid purpose for NULL.
This is also related to "isset". It can become an issue for the
way PHP processes form checkboxes.
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?


4
1,325 points at 100% Repute WDFplus Member
Posted February 5 '10 at 09:36 AM
      Posts: 8,436
I always default variables to null.

There are a few reasons for this...
  1. Empty String ("") is not the same as "no value". It has a 0-byte value assigned to it. Null has no value assigned to it.
  2. Empty String will also equate to a value in SQL. So if you insert an empty string into a nullable column, the inserted value will NOT be null.. this is important in differentiating records that were intentionally set to an empty value and records that are just not set.
  3. (boolean) "" equates to false whereas (boolean) null equates to null
  4. (int) "" equates to 0 whereas (int) null equates to null
  5. If you want to allow a value to be set to an empty string, there's no way with "" to check if the value has been changed from its default state. null allows the use of is_null() to differentiate.
  6. null also works for objects and arrays. For example, if i want a method that takes an array as a parameter, but it's optional, I can do function foo(array $bar = null) { }. Then you can test internally vs. null to see if the array has been provided.
  7. Lots of other little organizational benefits... it's always good to have more information than less.
Steven Moseley
President, Transio
smoseley is online now! Reply

5
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted February 5 '10 at 02:47 PM
      Posts: 3,100
LOL ...

medlington,
There you go ... the difference between a "transistor hardware tinkerer" (myself), and a "PHP programming nerd" (Steven).

I think the nerds will end-up ruling the Earth, so you might want to go with Steven
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?

6
36 points at 100%
medlington, WDF Addict! Home page   Private message  
Posted February 8 '10 at 09:07 AM
      Posts: 160
Thanks guys,

I would lable myself more of a tinkerer but the nerds normally do things properly so Il try to do things with Nulls in future

Post Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Tutorial: How to Denormalize a Normalized Table Using SQL smoseley Coding Articles & Tutorials 4 July 6 '09 01:59 AM
mysql help needed Demon Templates MySQL and MaxDB 1 February 11 '09 01:28 PM
Creating conditional foreign key relationships in MySQL TheGardener MySQL and MaxDB 6 October 13 '07 03:05 PM
JS menu alignment in IE robbieM Javascript, AJAX, and JSON 0 January 28 '05 09:33 AM