$a = true;
//b is unset;
switch(true) {
case ($a): echo 'a';
case (isset($b)): echo 'b';
default: echo 'c';
}
//outputs 'abc'. expected output is 'bc'
Why is the output not as expected?
Update: Expected output ‘bc’ is a typo and should read ‘ac’.
Update: I now see how this code produces different output than expected. EVERY code section in a switch block after the first case that returns true will be executed, unless the switch is ended by a break. After break point no other cases will be tested. This is where my mistake was as I expected testing to resume per case with the absence of break statements.
Code written by Isius is correct. It’s just the nice trick to find out, which of following cases is true.
Your code doesn’t work properly because you didn’t put break statement to each case.
It should look like that:
Output is ‘a’. I don’t know why you’re expecting to get “bc” output.