0

I'm not to keen on PHP and as such haven't spent much time learning the syntax, I was just wondering if this && operator would be syntactically correct in a switch case, so that I could have multiple cases match one case code block.

case "msg" && "username": 
/* enter the functions here */ 

break;
2
  • Nope. It'll evaluate to true Commented May 25, 2012 at 16:46
  • It is not. Make one case for each and just leave out the top case's "break;". It will fall through to the second case Commented May 25, 2012 at 16:47

1 Answer 1

8

No, that will not behave correctly. You can however do this:

case "msg":
case "username":
/* enter the functions here */

break;

Updated with Pekka and Marc's comments.

Sign up to request clarification or add additional context in comments.

5 Comments

No, that's grammatically correct. It'll parse and execute. it's not LOGICALLY correct.
Thanks for your response, I now understand and it was really silly of me to think the && operator would be allowed in a switch and case.
@Andrew Why do you think OP meant to use &&? What would that mean?
I see the OP was trying to make multiple cases for the switch to match to. I think they should have used || in the question. Nevertheless, in my googling, I was looking for how to do something like: case 'msg' && ($value >3):. Which is a completely different question but would be asked almost identically as how this question was asked, interestingly enough. In any case... it's probably bad design and I've already done something different.
@Andrew I see, the only languages that I know that support that is those that support pattern matching in switch statements. You would put those two conditions in a tuple and match on that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.