Jump to content

[SOLVED] If, Then... testing multiple settings of one variable


Errant_Shadow

Recommended Posts

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"?

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

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)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.