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.

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
{
    // ...
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.