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. Link to comment https://forums.phpfreaks.com/topic/161806-solved-switch-with-andinequality/ 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 { } Link to comment https://forums.phpfreaks.com/topic/161806-solved-switch-with-andinequality/#findComment-853717 Share on other sites More sharing options...
vigiw Posted June 11, 2009 Author Share Posted June 11, 2009 Easy enough if/else... thanks! Link to comment https://forums.phpfreaks.com/topic/161806-solved-switch-with-andinequality/#findComment-853721 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. Link to comment https://forums.phpfreaks.com/topic/161806-solved-switch-with-andinequality/#findComment-853722 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. Link to comment https://forums.phpfreaks.com/topic/161806-solved-switch-with-andinequality/#findComment-853725 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. Link to comment https://forums.phpfreaks.com/topic/161806-solved-switch-with-andinequality/#findComment-854365 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.