AliN11
AliN11

Reputation: 2583

php switch statement returns invalid output

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?

Upvotes: 0

Views: 483

Answers (7)

trincot
trincot

Reputation: 351328

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.

Upvotes: 0

Yuri Blanc
Yuri Blanc

Reputation: 645

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

Upvotes: 0

Jakir Hossain
Jakir Hossain

Reputation: 2517

Try this code.

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

Upvotes: -1

RST
RST

Reputation: 3925

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

Upvotes: 0

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111889

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.

Upvotes: 0

Professor Abronsius
Professor Abronsius

Reputation: 33823

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;
}

Upvotes: 2

u_mulder
u_mulder

Reputation: 54796

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;
}

Upvotes: 1

Related Questions