kreut Posted March 4, 2011 Share Posted March 4, 2011 Hi! Can someone please tell me the logical difference between if (isset($_POST['add_text'])) { blah blah blah and if ($_POST['add_text']) { blah blah blah Quote Link to comment https://forums.phpfreaks.com/topic/229542-whats-the-point-of-isset/ Share on other sites More sharing options...
btherl Posted March 4, 2011 Share Posted March 4, 2011 http://php.net/manual/en/types.comparisons.php isset() is typically used to see if a variable was passed through a form, even if it was passed as empty. But the second piece of code you've got checks if the value is "true" or not, which is defined according to some arcane rules listed in that table. Quote Link to comment https://forums.phpfreaks.com/topic/229542-whats-the-point-of-isset/#findComment-1182641 Share on other sites More sharing options...
ignace Posted March 4, 2011 Share Posted March 4, 2011 if(isset($variable)) { // only executes when $variable IS NOT NULL } if($variable) { // executes when $variable != false, 0, '', array(), .. } Quote Link to comment https://forums.phpfreaks.com/topic/229542-whats-the-point-of-isset/#findComment-1182747 Share on other sites More sharing options...
kreut Posted March 4, 2011 Author Share Posted March 4, 2011 Thank you both for your responses. To clarify, then, if I want to see if a particular form is sent in, I would check if that particular submit button "isset"; and if I want to see if a particular form item is not empty, I would then use if (!$variable_of_interest) --- would this be the standard way of checking these two form items? Quote Link to comment https://forums.phpfreaks.com/topic/229542-whats-the-point-of-isset/#findComment-1182751 Share on other sites More sharing options...
btherl Posted March 6, 2011 Share Posted March 6, 2011 More or less. While I trust php's "isset" and it is useful for exactly what you describe, I don't trust php's "is true". The big problem with it is that "0" is false. But 0 is a perfectly good number, and it's most definitely "not empty", even though both !0 and empty(0) are TRUE!! This quirk results in so many bugs, usually forms that mysteriously fail when particular input results in a 0 being tested for true-ness. And the biggest gotcha with isset() is that false and 0 are "set" values, because "set" refers to the variable itself being set and not the value. Quote Link to comment https://forums.phpfreaks.com/topic/229542-whats-the-point-of-isset/#findComment-1183752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.