문제

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?

도움이 되었습니까?

해결책 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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top