Jump to content

isset


mnvdk

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!
Link to comment
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].
Link to comment
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)
Link to comment
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).
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.