Page 1 of 1

Converting conditionals to switches

Posted: Mon Jul 18, 2005 4:51 pm
by theda
My question (yes... another question) is how do I convert:

Code: Select all

if (isset($HTTP_COOKIE_VARS['the']) AND empty($HTTP_GET_VARS['the'])) {
		$the = $HTTP_COOKIE_VARS['the'];
	} elseif (isset($HTTP_GET_VARS['the'])) {
		$the = $HTTP_GET_VARS['the'];
		setcookie('the',$the, time()+60*60*24*30);
	} else {
		setcookie('the','see', time()+60*60*24*30);
		$the = "see";
	}
into switch()

I'm trying to get rid of as many conditionals as possible and replacing them with switch() just so I can learn how to use them better ^_^.

Posted: Mon Jul 18, 2005 4:55 pm
by nielsene
switch's only replace conditionals of the form:

Code: Select all

if ($var=="value 1") {
} else if ($var=="value 2") {
} else if ($var=="value 3") {
else {
}
Where "value 1","value 2","value 3" are simple constant literals (strings, integers, etc) and the variable you are comparing is the same in all the cases.

In your case you're checking if multiple different variables are set -- this is not convertable to a switch.

Posted: Mon Jul 18, 2005 4:55 pm
by Burrito
can't really use isset() in a switch.

you could check to see if the var is set and then create a boolean var and switch() that, but seems kinda pointless to me...

probem with isset and switch is switch compares values and isset isn't looking for a value, just to determine if the variable is set period.

Posted: Mon Jul 18, 2005 4:56 pm
by Burrito
damn it! beaten again...grr!

gawd I really am getting old, and it's slowing me down...stupid 30's

Posted: Mon Jul 18, 2005 4:58 pm
by John Cartwright
I've done this a couple for times :roll: but I would consider it bad practice...

Code: Select all

switch (true) {
  case isset($var) :
      echo $var;
  break;
  case isset($var2) : 
      echo $var2;
  break;
}

Posted: Mon Jul 18, 2005 4:59 pm
by nielsene
[Off Topic]
well that's only a few hundred days older... but I guess that is aeons in "internet time"
[/off topic]

Posted: Mon Jul 18, 2005 5:03 pm
by nielsene
Jcart wrote:I've done this a couple for times, I would consider it bad practice though..

Code: Select all

switch (true) {
  case isset($var) :
      echo $var;
  break;
  case isset($var2) : 
      echo $var2;
  break;
}
Just wondering, why would you every want to use that construction? I'm actually suprised the interpretor doesn't barf on that, I wonder if you reported it as a bug if Zend would agree... :)

This seems simpler and clearer:

Code: Select all

if (isset($var)) {
  echo $var;
} else if (isset($var2)) {
  echo $var2;
}
I can't picture any use... You're not using the default, but if you were that falls into the final else. Perhaps if you needed some very interesting fall-through behavoir?

Posted: Mon Jul 18, 2005 9:12 pm
by theda
That's what I thought with this. I've used switches in other places in place of conditionals and they worked fine there, and was curious if it worked with isset() and empty(). I'll be leaving it the way it is if it's not really practical to change it.

Thanks for the comments though. :)