0

php switch statement returns invalid output :

$cond = "Night";

switch($cond){
    case "Morning":
    case "Night":
        $name = "Good";

    case "Morning" :
        echo $name." Morning";
        break;

    case "Night" :
        echo $name." Night";
        break;
}

output : Good Morning

Expected Good Night

Edit (after reading replies) :

Thanks for answers. I didn't get a clear answer.

I know i can use another statement like if else. But logically the code should work correctly(I think). because there's no break after 2 first cases .

but why when execution reaches to case "Morning" , the condition is true while isn't true in real?

3
  • 1
    There is not a break after the second case, so it will go through the next case, and there's a break on the next case, once your code sees the break, it'll exit from switch-case. Commented Jan 10, 2016 at 10:15
  • you said : ` and there's a break on the next case` this break shouldn't work if the case condition isn't true. Commented Jan 10, 2016 at 10:55
  • 1
    PHP doesn't evaluate any other case conditions once it has found a true one. It blindly continues executing statements falling through the cases until it finds a break. Commented Jan 10, 2016 at 11:28

7 Answers 7

2

You could do it slightly differently - as there is always going to be a good you could do this:

$cond = "Night";
$pre='Good ';

switch($cond){
    case "Morning" : echo $pre." Morning"; break;
    case "Night" : echo $pre." Night"; break;
}
Sign up to request clarification or add additional context in comments.

1 Comment

There are another cases . i created first case because the Morning and Night have similar conditions.
1

As you didn't add break first case - execution continues and breaks after

echo $name." Morning";

But even if you add break statement for the first case - you won't reach last case:

case "Night" :
    echo $name." Night";
    break;

as execution already out of switch-block.

So you have to write some other code with another logic. Simple one is:

$cond = "Night";

switch($cond){
    case "Morning":
    case "Night":
        echo "Good" . $cond;
        break;
}

Comments

0

The switch construct is always a bit of a risk when you do not specify a break, as execution will just continue to the next cases without testing those conditions.

As stated in the docs, I emphasise in bold:

It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case.

So, to be clear: PHP does not verify case conditions any more once it has started executing statements inside a case. Without a break, it will just continue with the statements of the next cases without verifying the case conditions. And so it goes from case to case, executing all statements until it finds a break or the end of the switch.

So the following code:

switch (1) {
case 1:
    echo "one ";
case 2:
    echo "two ";
case "hello":
    echo "hello ";
}

will output:

one two hello

PHP finds the first case condition to be true and starts executing statements without evaluating any other case conditions.

This may be very counter-intuitive, and also makes the code less readable. It is best practice to put a break at the end of each case to avoid any misunderstanding.

2 Comments

Thanks. but why PHP considers case "Morning"(3rd case) as true ? while isn't true.
It doesn't consider it as true, like it says in the docs, it just executes the statements without verifying anything until the next break. This is what I mean "risky" to use. But it is the same in other languages as well.
0

It seems the order here is important, if you used:

$cond = "Night";

switch($cond){
    case "Morning":
    case "Night":
        $name = "Good";

    case "Night" :
        echo $name." Night";
        break;        

    case "Morning" :
        echo $name." Morning";
        break;
}

you would get what you expect. However as you see it can't be easily predicted what would be exact result, so depending on your needs, I would do it in different way, for example I would use 2 switches for that:

$cond = "Night";
$name = '';
switch($cond){
    case "Morning":
    case "Night":
        $name = "Good";
        break;
}

switch ($cond) {
    case "Morning" :
        echo $name." Morning";
        break;    
    case "Night" :
        echo $name." Night";
        break;
}

or even simpler:

$cond = "Night";
$name = '';
switch($cond){
    case "Morning":
    case "Night":
        $name = "Good";
        break;
}

switch ($cond) {
    case "Morning" :
    case "Night" :
        echo $name." ".$cond;
        break;
}

Now everything will be obvious and noone will have difficulty to understand what's going on here.

Comments

0

To keep things organized you could do something like this. You will not need the switch

$cond = "Night";
$name = array (
     'Morning' => 'Good',
     'Night' => 'Good'
     );
echo $name[ $cond ]." ".$cond;

In this case you don't have to add another switch case for new situations, just add an entry to the $name array

2 Comments

I know i can do that using another statement like if and else if
If/else is not the same. But if the other way fits your logic better than that is okay. Just try to use every code only once, instead of with every case or every if/else. You will be happy for it when you want to change something. Using it the way I suggested is also faster, there is no checking involved.
0

If you don't break; your switch it will continue and step into next cases.

    $cond = "Night";

switch($cond){
    case "Morning":
    case "Night":
        $name = "Good";
        break; // add this

    case "Morning" :
        echo $name." Morning";
        break;

    case "Night" :
        echo $name." Night";
        break;
}

this happens also with other languages such as java, is called fall through

Comments

-1

Try this code.

$cond = "Night";
$name = "Good ";
switch($cond){
    case "Morning":
        echo $name . $cond;
    break;
    case "Night":
        echo $name . $cond;
    break;
}

Comments

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.