Jump to content

[SOLVED] Switch with And/Inequality


vigiw

Recommended Posts

Hello.. I'm working on a switch statement with multiple possible conditions...

 

Scenario 1:

"(($var1 < 0) AND ($var2 < 0))" ... [Then something echoing both to 0]

 

Scenario 2:

"(($var1 < 0)" ... [Then something echoing var 1 to 0, and var 2's value]

 

Scenario 3:

"(($var2 < 0)" ... [Then Something echoing var 2 to 0, and var 1's value]

 

Scenario 4 (Default):

[Default/Normal Condition] - Simply echoes the values of both vars.

 

When I try to get this going in a code, it simply doesn't work.  Any ideas on how I can go about doing this, if possible in switch statements?  I thought about if/else, but I have 4 different conditions to go through (if not Scenario 1, then 2... 3... 4... and I figured that was a bit much).

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/161806-solved-switch-with-andinequality/
Share on other sites

Or switch on true:

 

<?php
switch (true) {
case (($var1 < 0) && ($var2 < 0)):
	//do this
	break;
case ($var1 < 0):
	//do that
	break;
case ($var2 < 0):
	//do what?!
	break;
default:
	//do this
}
?>

Takes up a bit more code than if/elseif/elseif/else, but I find it cleaner sometimes.

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.