Jump to content

if to case conversion


Q695
Go to solution Solved by Psycho,

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

  • Solution

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 by Psycho
Link to comment
Share on other sites

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 by Q695
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.