Jump to content

Archived

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

mnvdk

isset

Recommended Posts

I was just wondering (while wandering)...

I've always used
if (!isset($variable))
to determine whether $variable is set or not.

I could also check this just by using
if (!$variable)
but I've always thought this was less "beautiful", or whatever...

But what is the major difference between the two ways?
I'm fairly new to PHP, so I'm not that into the technological specifics of every function, so if there is some major problem by just using
!$variable, I wouldn't know it.
Sarcasm: It looks like it works, therefore it's good! The IE way!

Share this post


Link to post
Share on other sites
basically, if you simply use [tt]if (!$variable)[/tt], it will work, but if you have error reporting turned on, you are actually accessing the variable before it is instantiated, and you'll produce warnings. it's much cleaner and more appropriate code to catch that error yourself with [tt]isset()[/tt].

Share this post


Link to post
Share on other sites
The first (isset) checks to see if the variable actually exists. The second, checks to see if the variables value is true or false.

Share this post


Link to post
Share on other sites
if you are using this to check values from a form becareful. the variable will be set if it was present in a form - even if the user didn't put any data in there.

I always use isset to check a whole list of vars are present then !empty for those that are required (or !is_null)

Share this post


Link to post
Share on other sites
[quote author=ToonMariner link=topic=110373.msg446008#msg446008 date=1159885608]
if you are using this to check values from a form becareful. the variable will be set if it was present in a form - even if the user didn't put any data in there.

I always use isset to check a whole list of vars are present then !empty for those that are required (or !is_null)
[/quote]

actually, not all variables in a form are set automatically. text fields are, checkboxes and radio buttons are not by default (unless, of course, you have a radio button checked by default).

Share this post


Link to post
Share on other sites

×

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.