Converting conditionals to switches

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

Converting conditionals to switches

Post 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 ^_^.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

damn it! beaten again...grr!

gawd I really am getting old, and it's slowing me down...stupid 30's
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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;
}
Last edited by John Cartwright on Mon Jul 18, 2005 4:59 pm, edited 1 time in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

[Off Topic]
well that's only a few hundred days older... but I guess that is aeons in "internet time"
[/off topic]
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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?
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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. :)
Post Reply