Q695 Posted May 13, 2013 Share Posted May 13, 2013 I'm stumped on how to convert the if statement to a case statement if ($page>=3 && $page<4) case (>='3' && <'4'): It's doing the block inside when the case variable isn't set. Link to comment https://forums.phpfreaks.com/topic/277965-if-to-case-conversion/ Share on other sites More sharing options...
Jessica Posted May 13, 2013 Share Posted May 13, 2013 Are you just trying to say 3, or is there a possibility that $page could be 3.5 or something? Link to comment https://forums.phpfreaks.com/topic/277965-if-to-case-conversion/#findComment-1429876 Share on other sites More sharing options...
Irate Posted May 13, 2013 Share Posted May 13, 2013 Are you just trying to say 3, or is there a possibility that $page could be 3.5 or something? lol Eh... try using variable assignments within the if clause and then refer to the variable with the case statement? Also, don't you use case statements only in switch statements? O_O Link to comment https://forums.phpfreaks.com/topic/277965-if-to-case-conversion/#findComment-1429893 Share on other sites More sharing options...
Barand Posted May 13, 2013 Share Posted May 13, 2013 if ($page>=3 && $page<4) converted to case statements becomes switch ($page) { case 3: // do something break; } Link to comment https://forums.phpfreaks.com/topic/277965-if-to-case-conversion/#findComment-1429894 Share on other sites More sharing options...
Jessica Posted May 13, 2013 Share Posted May 13, 2013 lol Eh... try using variable assignments within the if clause and then refer to the variable with the case statement? Also, don't you use case statements only in switch statements? O_O What exactly is so funny? Link to comment https://forums.phpfreaks.com/topic/277965-if-to-case-conversion/#findComment-1429906 Share on other sites More sharing options...
Jessica Posted May 13, 2013 Share Posted May 13, 2013 converted to case statements becomes switch ($page) { case 3: // do something break; } Which makes perfect sense - as long as he's really not expecting any other value other than 3. In which case why wasn't the original code if($page==3) instead of that weird greater than/less than combo? Link to comment https://forums.phpfreaks.com/topic/277965-if-to-case-conversion/#findComment-1429907 Share on other sites More sharing options...
Barand Posted May 13, 2013 Share Posted May 13, 2013 There's no answer to that (perhaps it was generated by Dreamweaver) - we'll have to ask Q695 Link to comment https://forums.phpfreaks.com/topic/277965-if-to-case-conversion/#findComment-1429918 Share on other sites More sharing options...
Psycho Posted May 13, 2013 Share Posted May 13, 2013 If the name of the variable is any indication, I assume $page will be an integer or should at least be forced to be an integer. But, if the value really can be a range from 3 up to just less than 4, you can still use a switch. This is a "pro tip" that a colleague shared with me years ago regarding switch statements. On each case statement within a switch you put the comparison for the switch value. If they are compared as equal (i.e. true) then the code for that case is executed. But, if you want to use a switch statement where each case comparison would be a range or possibly multiple comparisons, you can do it by using the switch in a non-intuitive way. Use the Boolean true as the switch value and the conditions as the case statements switch(true) { case ($page<1): //Perform some action for values 0.0 to 0.9... break; case ($page>=1 && $page<2): //Perform some action for values 1.0 to 1.9... break; case ($page>=2 && $page<3): //Perform some action for values 2.0 to 2.9... break; case ($page>=3 && $page<4): //Perform some action for values 3.0 to 3.9... break; default: //Perform some action for values over 4 break; } This comes in very handy when you have a fixed set of results that are dependent upon multiple conditions as opposed to using a lot of nested if/else statements. Link to comment https://forums.phpfreaks.com/topic/277965-if-to-case-conversion/#findComment-1429922 Share on other sites More sharing options...
Q695 Posted May 14, 2013 Author Share Posted May 14, 2013 Yes, page will be a rational number, and doing an include within an include, like 3.1, and 3.2 would be elements of 3. I was trying both ways, and displaying the code for the types of things I tried. It's still in the range. Link to comment https://forums.phpfreaks.com/topic/277965-if-to-case-conversion/#findComment-1430028 Share on other sites More sharing options...
Jessica Posted May 14, 2013 Share Posted May 14, 2013 HA! MY QUESTION WAS VALID. Link to comment https://forums.phpfreaks.com/topic/277965-if-to-case-conversion/#findComment-1430029 Share on other sites More sharing options...
Q695 Posted May 15, 2013 Author Share Posted May 15, 2013 I know it was, so I was saying that the number could be a rational number also. Link to comment https://forums.phpfreaks.com/topic/277965-if-to-case-conversion/#findComment-1430110 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.