newera Posted May 28, 2012 Share Posted May 28, 2012 Hi guys. Is it really necessary to set the $name,$email,$gender to NULL? If they are empty, aren't they null then? Thanks! if (!empty($_REQUEST['name'])) { $name = $_REQUEST['name']; } else { $name = NULL; echo '<p class="error">You forgot to enter your name!</p>'; } Link to comment Share on other sites More sharing options...
Larry Posted May 28, 2012 Share Posted May 28, 2012 No, empty and null are different things. Empty in PHP can be an empty string, the number 0, or the value false. null is a non-value that's different than those. Link to comment Share on other sites More sharing options...
newera Posted May 28, 2012 Author Share Posted May 28, 2012 Thanks Sir, finding your book really helpful! I'm still a bit confused of the following. If nothing is typed into a particular input field in the form,doesn't that mean that the corresponding variable is unset and therefore Null? Link to comment Share on other sites More sharing options...
Larry Posted May 30, 2012 Share Posted May 30, 2012 Thanks for the nice words. No, if nothing is entered into that text input, then it's value will be an empty string, not null. Link to comment Share on other sites More sharing options...
HartleySan Posted May 31, 2012 Share Posted May 31, 2012 Maybe not the best analogy, but someone once said that, "'Empty' is like a burnable CD without any data on it, and 'null' is like no CD at all." Does that help any? Link to comment Share on other sites More sharing options...
newera Posted May 31, 2012 Author Share Posted May 31, 2012 Hmm thanks Larry and HartleySan (yes, it helps). For the following piece of code, is setting the gender to NULL necessary or is it just a good programming practice? (since if gender is not set, then it automatically means its NULL) Thanks! if (isset($_REQUEST['gender'])) { $gender = $_REQUEST['gender']; } else { $gender = NULL; } Link to comment Share on other sites More sharing options...
margaux Posted May 31, 2012 Share Posted May 31, 2012 If you are going to use $gender later in your script, you need to create it otherwise you will get an error '$gender undefined' which is not the same as $gender = NULL 1 Link to comment Share on other sites More sharing options...
newera Posted June 1, 2012 Author Share Posted June 1, 2012 Ah I see, Thanks margaux! Another small doubt I have is on pg87, on script 3.5, shouldn't the form validation for $_POST['distance'] be using !empty function instead of isset since it's a text input ? Link to comment Share on other sites More sharing options...
HartleySan Posted June 1, 2012 Share Posted June 1, 2012 I don't have the book handy, but perhaps the following link will help: http://virendrachandak.wordpress.com/2012/01/21/php-isset-vs-empty-vs-is_null/ Link to comment Share on other sites More sharing options...
Recommended Posts