monkeytooth Posted February 25, 2010 Share Posted February 25, 2010 I have a search index I am building up. It can be accessed one of 2 ways first way being your customary way going to the main index of the search page and typing a term and hitting a button to search, then next would be via URL through variables contained within the URL. What I want to do is figure out what the best way to handle an instance where both the _GET and _POST values are empty, null, not supplied, however you want to spin it. And display a message saying "Please enter your search term". My debate is what is the best way to handle that? I don't know what would be the best way I guess is the best way to put it should I do something like if((!isset($_POST[var1])) AND (!isset($_GET[var1]))){/*show default message*/} or if((empty($_POST[var1])) AND (empty($_GET[var1]))){/*show default message*/} or if((!$_POST[var1]) AND (!$_GET[var1])){/*show default message*/} is there a better way to approach this idea? Quote Link to comment https://forums.phpfreaks.com/topic/193323-isset-vs-empty/ Share on other sites More sharing options...
alpine Posted February 25, 2010 Share Posted February 25, 2010 I prefer to expect variable either as POST or GET and try to avoid the possibility for both, if you however expect both you could use REQUEST <?php if(empty($_REQUEST['var'])) ?> as empty returns false also if 'var' is not set, so it checks both if it is set and that its not empty Quote Link to comment https://forums.phpfreaks.com/topic/193323-isset-vs-empty/#findComment-1017937 Share on other sites More sharing options...
Wolphie Posted February 25, 2010 Share Posted February 25, 2010 Well as you know, isset() literally just checks if the variable has been set, it has no kind of input validation on it. For example you would use it to check to see if a session variable had been set when a user logs in, the value may not be important. However, in a user-submitted form the value is important, i'd prefer to use the empty() function to check against form element inputs. But on the other hand, I would use isset() to check for checkboxes, and radio buttons. Quote Link to comment https://forums.phpfreaks.com/topic/193323-isset-vs-empty/#findComment-1017938 Share on other sites More sharing options...
monkeytooth Posted February 25, 2010 Author Share Posted February 25, 2010 Well I do use empty typically when it comes to user input, example I check to see if the form submit button is part of the posted inputs.. if its not empty then I double check it if its not empty with the isset and if isset does it match the value I want it to match. But thats not fully the case here, I need to check both get and post and verify if they are null or set Quote Link to comment https://forums.phpfreaks.com/topic/193323-isset-vs-empty/#findComment-1017941 Share on other sites More sharing options...
Wolphie Posted February 25, 2010 Share Posted February 25, 2010 Oh, sorry I misunderstood. As alpine pointed out, using $_REQUEST will do both if you're unsure which to expect. Quote Link to comment https://forums.phpfreaks.com/topic/193323-isset-vs-empty/#findComment-1017944 Share on other sites More sharing options...
monkeytooth Posted February 25, 2010 Author Share Posted February 25, 2010 Well I seemed to skip over that response while scrolling down initially. All variables expected to trigger an event are named differently ie $_POST['var1'] $_GET['var2'].. however with either triggering an event accordingly I want to catch it if both are not there. independently I can catch them fine, trigger a message if its not there, run script if it is. But in the past when playing with $_GET $_POST data, ive had to use !$_POST, or !isset() or empty() and occasionally ran into an issue with each one what would work on one page/server (i have multiple clients and servers to work with) All the servers to my knowing are set up similar, same php builds an all else different companies in some cases but generally all the same stuff. Either way !$_POST would work on one where !isset() would work on the other.. or only isset() would work but not !isset() So I was trying to find out a better more universal approach one that is in good practice so to speak. Quote Link to comment https://forums.phpfreaks.com/topic/193323-isset-vs-empty/#findComment-1017953 Share on other sites More sharing options...
Wolphie Posted February 25, 2010 Share Posted February 25, 2010 Well, I still don't entirely understand what you're trying to achieve, but this is my best guess: <?php $validate = array('var1', 'var2'); foreach ($validate as $key) { if (!isset($_REQUEST[$key]) || empty($_REQUEST[$key])) { // empty or not set } else { // They're fine } ?> Quote Link to comment https://forums.phpfreaks.com/topic/193323-isset-vs-empty/#findComment-1017959 Share on other sites More sharing options...
Mchl Posted February 25, 2010 Share Posted February 25, 2010 One thing to remember is that calling empty on variable that is not set, will result in E_NOTICE level error. If you want your code to be all nice and neat, you should always use isset before empty Quote Link to comment https://forums.phpfreaks.com/topic/193323-isset-vs-empty/#findComment-1017967 Share on other sites More sharing options...
alpine Posted February 25, 2010 Share Posted February 25, 2010 $_GET, $_POST and $_REQUEST are all predefined variables, and returns empty if not set - and empty array "array()" is considered valid empty using empty() Quote Link to comment https://forums.phpfreaks.com/topic/193323-isset-vs-empty/#findComment-1017988 Share on other sites More sharing options...
Mchl Posted February 25, 2010 Share Posted February 25, 2010 Well... yeah... there's no point checking isset($_POST) byt checking isset($_POST['variable']) is something quite another. Scratch all that... something must have changed since I checked it last time. Checking empty on unset variable does not raise E_NOTICE in PHP 5.3 or 5.2. Have nowhere to check previous versions. Quote Link to comment https://forums.phpfreaks.com/topic/193323-isset-vs-empty/#findComment-1018030 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.