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"? Link to comment https://forums.phpfreaks.com/topic/143753-solved-if-then-testing-multiple-settings-of-one-variable/ 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 Link to comment https://forums.phpfreaks.com/topic/143753-solved-if-then-testing-multiple-settings-of-one-variable/#findComment-754240 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 Link to comment https://forums.phpfreaks.com/topic/143753-solved-if-then-testing-multiple-settings-of-one-variable/#findComment-754246 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. Link to comment https://forums.phpfreaks.com/topic/143753-solved-if-then-testing-multiple-settings-of-one-variable/#findComment-754247 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) Link to comment https://forums.phpfreaks.com/topic/143753-solved-if-then-testing-multiple-settings-of-one-variable/#findComment-754249 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.