Question

I have following code:

switch(true)
    {
    case (something):
        {
        break;
        }
    case (something2):
        {
        break;
        }
    case (something3):
        {
        break;
        }
    }

Also the switch statement must check what one of cases give a TRUE, that is not the problem, the problem is, that i have now a case, where inside of case ... break; after checking other data, i wish to choose other switch-case, the one, that following him. I have try do this:

switch(true)
    {
    case (something):
        {
        break;
        }
    case (something2):
        {
        if(check)
            {
            something3 = true;
            continue;
            }
        break;
        }
    case (something3):
        {
        break;
        }
    }

But PHP do not wish to go in to the case (something3): its break the full switch statement. How i can pass the rest of code of one case and jump to the next?

Was it helpful?

Solution 2

Using your code:

switch(true) 
{
    case (something):
    {
        break;
    }
    case (something2):
    {
        if(check) {
            something3 = true;
        }
        else {
            break;
        }
    } 
    case (something3):
    {
        break;
    }
}

This will get to case something2 and run your check. If your check passes then it doesn't run the break statement which allows the switch to "fall through" and also do something3.

OTHER TIPS

This is what's called a "fall-through". Try organizing your code with this concept.

switch (foo) {
  // fall case 1 through 2
  case 1:
  case 2:
    // something runs for case 1 and 2
    break;
  case 3:
    // something runs for case 3
    break;
}
case (something2):
    {
    if(!check)
    {
        break;
    }
}

Try this:

switch(true) {
    case (something):
    {
        break;
    {
    case (something2):
    {
        if (!check) {
            break;
        }
    }
    case (something3):
    {
        break;
    }
}

I had some similar situation and I came up with a solution which in your case would be like this:

switch(true)
{
    case (something):
    {
        break;
    }
    case (something2):
    {
        if(!check)
        {
            break;
        }
    }
    case (something3):
    {
        break;
    }
}

You see that instead of checking the conditions to go to the next case, we checked if we should ignore the current one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top