isset() VS empty()

25 04 2006

It might be confusing as far as i red on many forums that fails to explain the diffrence between isset() and empty().
So let me put it this way :

 

Isset() checks if a variable has a value including ( Flase , 0 , or Empty string) , But not NULL.
Returns TRUE if var exists; FALSE otherwise.

 

On the other hand the empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value.

 

Comparison example on both isset() and empty()

 

<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
?>

 

For more information and examples visit the following links.

http://www.php.net/isset

http://www.php.net/empty

 

m0o


Actions

Information

30 responses

13 01 2009
Yunus

Thanks for information. Now, I know differences between them

27 03 2009
someone somewhere

empty also serves a purpose for arrays quite well

27 03 2009
someone somewhere

to add to that isset is pretty much useless other then for sessions i find that in that case isset many times is more ideal although empty is normally fool proof

29 10 2009
Felix

Good point there. But nobody should be confused about it. The function name itself will suggest how it will be used.

19 08 2010
Eduardo

what happens if ” $var=null; ” what does return isset??

27 11 2010
A.Jesin

Thanks for the info I was able to implement it in my PHP script and create a single page with a HTML form and PHP code for entering it into the database. If the values are “isset” it goes into the PHP code else displays HTML form.
Inside the if loop if the values are not empty they are entered in the database else a msg is displayed “Fields cannot be blank” 🙂

2 12 2010
Manikandan

My doubt is clear after read your article regarding empty() and isset() thank you.

5 12 2010
Lenny

Great advise. For my $_GET / POST / REQUEST I use this simple function to automatically check if the value is set and avoid any undefined index warnings.


//GET
function GV($key,$default=""){
return (isset($_GET[$key])) ? $_GET[$key] : $default;
}

I did a quick write up about using this function here: quick php get post request isset function

16 01 2011
PHP Freak

Good article. Just found another over at Tutorial Arena that takes a more in depth look at the difference between the two: PHP isset vs empty

Hope these 2 articles help someone.

8 05 2011
Leo

The one at Tutorial Arena complements this one nicely.

12 05 2011
loopduplicate

thanks. very concise!

11 06 2011
Jeremy

Thanks a bunch! I used to think they were the same, but saw some example texts that stated otherwise, so I searched google for why and found this.

16 08 2011
Stefan

Ahh tricksters! Thanks for clarifying this, just been going in circles for a few mins with a DB value of NULL, who would have thought! Thanks for the post

1 09 2011
joelpittet

Also just to save some space and reduce duplication errors. empty() implicitly calls isset() and you don’t need both since PHP 5.1.0

// Wrong
if ($var_notdefined)

// Right
if (isset($var_notdefined))

// Right but could cause duplication errors of the same variable and long
if (isset($var_notdefined) && ! empty($var_notdefined))

// Preferred for projects requiring PHP 5.1+
if ( ! empty($var_notdefined))

See notes on __isset() http://ca3.php.net/empty

23 11 2011
Hung Nguyen

at the first time read your post, I was a little bit confused, but then it clearer. thank you ^^

1 02 2012
MRWBSN

Reblogged this on kodemoo and commented:
thx for the explanation. this is very helpful. 😀

24 11 2012
Felipa

I was curious if you ever thought of changing the page layout of your
website? Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content
so people could connect with it better. Youve
got an awful lot of text for only having one or 2 pictures.
Maybe you could space it out better?

12 05 2013
mike chang six pack shortcuts tpb

😮 I hope individuals are smart sufficient to know their limit when on creatine.
You do not need to lift HEAVY to get grow to be stronger.

7 06 2013
acai ultra lean reviews

Greetings! Very useful advice in this particular article!
It’s the little changes that produce the largest changes. Many thanks for sharing!

22 06 2013
Max Lean X Build Muscle

Every weekend i used to pay a visit this web site, as
i want enjoyment, since this this web site conations actually good funny information too.

24 06 2013
Maxx Test 300 Testosterone

Great web site. A lot of helpful info here. I’m sending it to a few friends ans also sharing in delicious. And of course, thanks for your effort!

3 07 2013
cash advance online

Simply want to say your article is as astonishing. The clarity on
your put up is just spectacular and that i could
assume you are an expert in this subject. Fine together with your permission allow me to grab your RSS feed to stay updated with approaching post.
Thanks one million and please carry on the enjoyable work.

23 09 2013
http://jillsink.typepad.com

I visited many websites however the audio feature for audio songs current at
this site is in fact fabulous.

18 03 2015
naved

Hi thanks ,
Here is also gud explan the difference between these two functions .. http://phpsollutions.blogspot.com/2015/03/difference-between-isset-and-empty.html

8 07 2016
turn key profit

Pretty! This was an extremely wonderful article. Thank you for supplying these
details.

7 10 2020
我应该在哪里使用isset()和!empty()|php问答

[…] isset vs.!empty […]

18 02 2022
In where shall I use isset() and !empty() - PhotoLens

[…] isset vs. !empty […]

14 03 2022
7 10 2022
16 12 2022
In where shall I use isset() and !empty()

[…] isset vs. !empty […]

Leave a reply to joelpittet Cancel reply