Web Design Forums

PHP

Have questions about PHP? Ask them here and our experts will assist you before you know it! You can also find help in the documentation at PHP.net.

NULL or "" ?



Site of the Month Nominations
ENTER YOUR SITE NOW!

Reply
 
LinkBack Thread Tools
Old February 5 '10, 07:19 AM (#1)
medlington is offline
WDF Regular
 
medlington's Avatar
 
Join Date: November 2005
Location: Sheffield, UK
Posts: 192
medlington is on a distinguished road
NULL or "" ?

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:

PHP Code:
$variable ""
Recently I came across the NULL and have realised I could declare the variable like this:

PHP Code:
$variable NULL
Whats the difference, will I get problems doing it one way over the other?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 5 '10, 09:39 AM (#2)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 5 '10, 09:52 AM (#3)
medlington is offline
WDF Regular
 
medlington's Avatar
 
Join Date: November 2005
Location: Sheffield, UK
Posts: 192
medlington is on a distinguished road
Thanks mate,

Nice to get some clarification. Il keep doing it the way I have been
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 5 '10, 10:36 AM (#4)
smoseley is offline
WDF Moderator
 
smoseley's Avatar
 
Join Date: March 2003
Location: Miami, FL
Posts: 8,796
smoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud ofsmoseley has much to be proud of
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 5 '10, 03:47 PM (#5)
mlseim is offline
WDF Staff
 
mlseim's Avatar
 
Join Date: April 2004
Location: Cottage Grove, Minnesota
Posts: 3,401
mlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud ofmlseim has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 8 '10, 10:07 AM (#6)
medlington is offline
WDF Regular
 
medlington's Avatar
 
Join Date: November 2005
Location: Sheffield, UK
Posts: 192
medlington is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Programming Help » PHP

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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 Database Systems Help 1 February 11 '09 02:28 PM
Creating conditional foreign key relationships in MySQL TheGardener Database Systems Help 6 October 13 '07 03:05 PM
JS menu alignment in IE robbieM Javascript, AJAX, and JSON 0 January 28 '05 10:33 AM

 
User Infomation
Your Avatar

Site Of The Month

Ticket Cake
Ticket Cake

Ticket Cake is a drupal based event ticketing platform. It features that ability to browse events and share them.

Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 02:43 AM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163