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

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.