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. Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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; } Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted May 13, 2013 Share Posted May 13, 2013 (edited) There's no answer to that (perhaps it was generated by Dreamweaver) - we'll have to ask Q695 Edited May 13, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted May 13, 2013 Solution Share Posted May 13, 2013 (edited) 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. Edited May 13, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
Q695 Posted May 14, 2013 Author Share Posted May 14, 2013 (edited) 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. Edited May 14, 2013 by Q695 Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 14, 2013 Share Posted May 14, 2013 HA! MY QUESTION WAS VALID. Quote Link to comment 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. 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.