Jump to content

When to use isset() and when to use empty()


dennismonsewicz

Recommended Posts

hmmm good question. I'd like to see a good answer.

 

From the manual anyways:

 

empty()

 

The following things are considered to be empty:

 

"" (an empty string)

0 (0 as an integer)

"0" (0 as a string)

NULL

FALSE

array() (an empty array)

var $var; (a variable declared, but without a value in a class)

 

Where as isset():

 

any of the above would be considered true except NULL I think or the variable not existing.

 

 

I think it really depends what you are trying to compare.

Link to comment
Share on other sites

You use them for just what they sound like they should be used for. isset is used if you want to see that a variable is set, and empty is to see if a variable isn't null. isset should be used when checking for variables that have a possibility to be unset (like when a form is submitted), because it's the only way to test if a variable is set without getting a notice if it isn't.

 

$var = '';
if(isset($var)) // true
{
    // ...
}

if(!empty($var)) // false
{
    // ...
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.