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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.