mnvdk Posted October 3, 2006 Share Posted October 3, 2006 I was just wondering (while wandering)...I've always usedif (!isset($variable))to determine whether $variable is set or not.I could also check this just by usingif (!$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 https://forums.phpfreaks.com/topic/22874-isset/ Share on other sites More sharing options...
obsidian Posted October 3, 2006 Share Posted October 3, 2006 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 https://forums.phpfreaks.com/topic/22874-isset/#findComment-103055 Share on other sites More sharing options...
trq Posted October 3, 2006 Share Posted October 3, 2006 The first (isset) checks to see if the variable actually exists. The second, checks to see if the variables value is true or false. Link to comment https://forums.phpfreaks.com/topic/22874-isset/#findComment-103056 Share on other sites More sharing options...
ToonMariner Posted October 3, 2006 Share Posted October 3, 2006 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 https://forums.phpfreaks.com/topic/22874-isset/#findComment-103077 Share on other sites More sharing options...
obsidian Posted October 3, 2006 Share Posted October 3, 2006 [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 https://forums.phpfreaks.com/topic/22874-isset/#findComment-103078 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.