Errant_Shadow Posted February 4, 2009 Share Posted February 4, 2009 Hi, I'm just curious if there is a shorter way to test one variable for multiple settings. Here's the scenario: // basically, I'm testing a variable to make sure it is set to an existing page, and if not, set the page to default if($_GET['page'] != "page1" || $_GET['page'] != "page2" || $_GET['page'] != "page3" || $_GET['page'] != "page4"){ $page = "default"; } // end if($_GET['page']) Is there any way I can write this so that I only enter "$_GET['page']" once, and then check it against multiple possible settings as seen above, or am I stuck writing out each statement in between each "OR"? Quote Link to comment Share on other sites More sharing options...
Snart Posted February 4, 2009 Share Posted February 4, 2009 switch($_GET['page']) { case "page1": case "page2": //Do something break; case "page3": //Do something else break; default: //in all other cases break; } EDIT: spelling mistake Quote Link to comment Share on other sites More sharing options...
Errant_Shadow Posted February 4, 2009 Author Share Posted February 4, 2009 I thought about using a switch() statement, but I don't need different things to happen for each case. I want to make sure someone doesn't enter an invalid page variable into the url and break my code, so I need to check the page variable present against all possible pages that my script can load Like, if my page can load "game" "about" and "terms" I don't want someone entering "?page=fubar" and messing it all up. if they do, I need my script to reset page to default Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2009 Share Posted February 4, 2009 Create an array with the list of values and use in_array to determine if the GET variable matches one of the values. Quote Link to comment Share on other sites More sharing options...
Errant_Shadow Posted February 4, 2009 Author Share Posted February 4, 2009 I see, I see... so... something like this? // set available pages in $pageArray $pageArray = array("page1", "page2", "page3", "page4"); // test posted data against $pageArray // if invalid, reset $page to "default" if(!in_array($_GET['page'], $pagesArray){ $page = "default"; } // end if(!in_array($_GET['page'], $pagesArray) 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.