Jump to content
Larry Ullman's Book Forums

Recommended Posts

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>';
}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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;
}

Share this post


Link to post
Share on other sites

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

  • Upvote 1

Share this post


Link to post
Share on other sites

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 ?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...