Array's and Conditional's

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Array's and Conditional's

Post by theda »

I decided "Hmm, let's get rid of this little hole in my script!" And what is that hole mind you? Conditional isset functions!

Example:
http://www.example.com/page.php?variable=notsomething
Choices are either something or somethingelse... You type in notsomething... But the script should default to something if variable doesn't equal either something or somethingelse.

My feeble attempt didn't go as planned... I'm missing something.

Code: Select all

// Conditional Setcookie - Theme
	if (isset($HTTP_COOKIE_VARS['the']) AND empty($HTTP_GET_VARS['the'])) {
		$the = $HTTP_COOKIE_VARS['the'];
	} elseif (isset($HTTP_GET_VARS['the'])) {
		$arr = array('erd','gra','ora','fli','low','ros','see');
		if($the == $arr) {
			$the = $HTTP_GET_VARS['the'];
			setcookie('the',$the, time()+60*60*24*30);
		} else {
			setcookie('the','gra', time()+60*60*24*30);
			$the = 'gra';
		}
	} else {
		setcookie('the','gra', time()+60*60*24*30);
		$the = 'gra';
	}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

if(($_GET['variable'] != "something") || ($_GET['variable'] != "somethingelse")
{
   die("invalied $_GET variable");
}
Or perhaps you're checking to see if $the is in the array

Code: Select all

if(in_array($the,$array)
{
  // proceed
} ELSE
{
   die();
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: Array's and Conditional's

Post by nielsene »

I think I'd recode this to:

Code: Select all

// Conditional Setcookie - Theme
if (isset($_COOKIE['the']) && !isset($_GET['the'])) {
    $the = $_COOKIE['the'];
} elseif (isset($_GET['the'])) {
    $arr = array('erd','gra','ora','fli','low','ros','see');
    if (in_array($the,$arr) {
        $the = $_GET['the'];
        setcookie('the',$the, time()+60*60*24*30);
    } else {
        setcookie('the','gra', time()+60*60*24*30);
        $the = 'gra';
    }
} else {
    setcookie('the','gra', time()+60*60*24*30);
    $the = 'gra';
}
Notice the two "real" changes-- the first call to empty was changed to a !isset and the in_array test in the deepest if statement
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

And you harrassed my deprecated Globals! Lol...
Post Reply