FlyingIsFun1217 Posted July 13, 2011 Share Posted July 13, 2011 Hey, I'm trying to get a page that goes through different situations via the GET variable. Because of this, I need to be able to deal with not having anything in the GET array (if the user just goes to x.php). The way I have it now is something like such: if (!array_key_exists('page', $_GET) && !empty($_GET)) { //Put in template code for error page print 'Wrong GET name!'; } else { //Page 1 - Welcome, process explanation if($_GET['page'] == "1" || empty($_GET)) { //Page one output! print "Page 1 / Empty"; } } Now, I know there's something wrong here, in the fact that if I run the script without passing a GET variable, I get a warning/error, because $_GET['page'] isn't set. How can I get around this? Is there no way to supply results for both scenarios? Thanks FlyingIsFun1217 Quote Link to comment Share on other sites More sharing options...
teynon Posted July 13, 2011 Share Posted July 13, 2011 if (isset($_GET)) { if (!array_key_exists('page', $_GET) && !empty($_GET)) { //Put in template code for error page print 'Wrong GET name!'; } else { //Page 1 - Welcome, process explanation if($_GET['page'] == "1" || empty($_GET)) { //Page one output! print "Page 1 / Empty"; } } } else { // Do this when get is not present. } Quote Link to comment Share on other sites More sharing options...
FlyingIsFun1217 Posted July 13, 2011 Author Share Posted July 13, 2011 So GET can be set, but still be empty? That would be the case with the "Page 1" if conditional. FlyingIsFun1217 Quote Link to comment Share on other sites More sharing options...
xyph Posted July 13, 2011 Share Posted July 13, 2011 Use empty() empty() won't throw an error if you pass a variable that doesn't exist into it. Quote Link to comment Share on other sites More sharing options...
FlyingIsFun1217 Posted July 13, 2011 Author Share Posted July 13, 2011 Basically, the problem I have with any of these proposed solutions is that an error is thrown when I run the script and don't supply a GET variable in the URL (which is a possible case in real-world use). When the PHP interpereter sees that I am referencing $_GET['post'], and it has nothing in that GET array, it freaks out and throws me an error. Is this maybe just my server/PHP setup? FlyingIsFun1217 Quote Link to comment Share on other sites More sharing options...
teynon Posted July 13, 2011 Share Posted July 13, 2011 I didn't read into the question very much. You're getting the error because your checking GET. I thing GET is going to exist no matter what. I'd have to check that though. You need to do an empty check for the specific variable. if (empty($_GET['page'])) { Edit: Just checked. POST ans GET always exist. (At least in my case.) If you wanted to see if any variables were passed, you could do this: if (count($_GET) == 0) { // No variables } Quote Link to comment Share on other sites More sharing options...
xyph Posted July 13, 2011 Share Posted July 13, 2011 error_reporting(-1); ini_set('display_errors', '1'); if (!array_key_exists('page', $_GET) && !empty($_GET)){ echo 'foo'; } else { echo 'bar'; } Your logic for checking is odd though. You probably want this instead error_reporting(-1); ini_set('display_errors', '1'); if ( !isset($_GET['page']) ) { //Put in template code for error page print 'Page does not exist'; } else { //Page 1 - Welcome, process explanation if( $_GET['page'] == "1" || empty($_GET['page']) ) { //Page one output! print "Page 1 / Empty"; } else { print "Page is {$_GET['page']}"; } } Gives me no errors with no query string, with ?page= and with ?page=foobar Quote Link to comment 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.