vigiw Posted June 11, 2009 Share Posted June 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
haku Posted June 11, 2009 Share Posted June 11, 2009 if(something) { } elseif(something else) { } elseif(another thing) { } else //default { } Quote Link to comment Share on other sites More sharing options...
vigiw Posted June 11, 2009 Author Share Posted June 11, 2009 Easy enough if/else... thanks! Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 11, 2009 Share Posted June 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
vigiw Posted June 11, 2009 Author Share Posted June 11, 2009 It does appear neater , but thanks for the suggestions.. good for future ref. Quote Link to comment Share on other sites More sharing options...
haku Posted June 12, 2009 Share Posted June 12, 2009 Interesting thebadbad - never seen that before. I may have to try it sometime. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.