The14thGOD Posted April 20, 2007 Share Posted April 20, 2007 I was wondering, can you use logic in case statements? ie switch($variable){ case($variable<10): blah blah blah } Thanks the14thgod Quote Link to comment https://forums.phpfreaks.com/topic/47884-logic-in-switch-statement/ Share on other sites More sharing options...
bobleny Posted April 20, 2007 Share Posted April 20, 2007 You know, some times the best way to find something like that out, is to simply try try it. I think you should try and report back when you've got the answer. Quote Link to comment https://forums.phpfreaks.com/topic/47884-logic-in-switch-statement/#findComment-234004 Share on other sites More sharing options...
The14thGOD Posted April 20, 2007 Author Share Posted April 20, 2007 ya, id try it, but im currently in class at the moment, and for some reason their wireless wont connect to my server i didn't want to code it all based on a switch if it did not work. so ill probably be sticking with if statements. Quote Link to comment https://forums.phpfreaks.com/topic/47884-logic-in-switch-statement/#findComment-234022 Share on other sites More sharing options...
Jenk Posted April 20, 2007 Share Posted April 20, 2007 Hint: <?php switch (true) { case ($var < 10): doSomething(); break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47884-logic-in-switch-statement/#findComment-234023 Share on other sites More sharing options...
The14thGOD Posted April 20, 2007 Author Share Posted April 20, 2007 thank you Quote Link to comment https://forums.phpfreaks.com/topic/47884-logic-in-switch-statement/#findComment-234026 Share on other sites More sharing options...
per1os Posted April 20, 2007 Share Posted April 20, 2007 Switch case logic is pretty simple here is a more thorough example: <?php $action = "post"; switch ($action) { case 'post': print "This is the post case selection"; break; case 'index': print "This is the index case selection"; break; default: case 'none': print "This is the default/none case selection"; break; case 'test1': case 'test2': print "Either test1 or test2 got you here!"; break; } ?> It does not have to be words it can be integers etc. But basically it is like an if statement, just a different style, IE: IF $action is equal to a certain item than execute that until the break, at which point do not execute more code. Quote Link to comment https://forums.phpfreaks.com/topic/47884-logic-in-switch-statement/#findComment-234028 Share on other sites More sharing options...
neel_basu Posted April 20, 2007 Share Posted April 20, 2007 You Cant insert Contitions (!, ==, >, <, <=, >= ,etc..) in case just values. you can enter condition in switch() only not in cases Use <?php switch($variable){ case 0: case 1: .............. case 9: case 10: blah blah blah break; default: echo 'Greater than 10 or smaller than 0'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47884-logic-in-switch-statement/#findComment-234155 Share on other sites More sharing options...
taith Posted April 20, 2007 Share Posted April 20, 2007 You Cant insert Contitions (!, ==, >, <, <=, >= ,etc..) in case just values. you can enter condition in switch() only not in cases Use <?php switch($variable){ case 0: case 1: .............. case 9: case 10: blah blah blah break; default: echo 'Greater than 10 or smaller than 0'; } ?> not completly true... if you switch(true){ case "$v<6": break; } it will take... basically the switch will grab the first case in which the "$v<6"===true Quote Link to comment https://forums.phpfreaks.com/topic/47884-logic-in-switch-statement/#findComment-234158 Share on other sites More sharing options...
neel_basu Posted April 20, 2007 Share Posted April 20, 2007 CONDITIONS CANNOT STAY IN CASE. CONDITIONS ALWAYS STAY IN switch() and if your contition in switch() is true. with respecty to respective cases / default cases. it will go through that class. and here switch is already true. no conditions at all. and as there is only 1 case it is entering in that case caues there is nothing to make it false. Quote Link to comment https://forums.phpfreaks.com/topic/47884-logic-in-switch-statement/#findComment-234169 Share on other sites More sharing options...
neel_basu Posted April 20, 2007 Share Posted April 20, 2007 Yes it will work. <?php $v = 8; switch(true){ case $v<6: echo 'Entering into case 1'; break; case $v>6: echo 'Entering into case 2'; break; default: echo 'Entering into default case'; } ?> But here also the condition is not in the CASE(s). Here the condition is in switch and the condition is true. Quote Link to comment https://forums.phpfreaks.com/topic/47884-logic-in-switch-statement/#findComment-234174 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.