Originally posted by TTT
of course, first case is true --> break
first case:
case ($beer_days>20 && $beer_days != 0):
So 0>20 in your world?
PHP's loose typing caught you out (it caught me out, too). testing ($beer_days) when $beer_days is zero will be false, because 0==false. 0>20 is also false, so that's the case that matches.
When $beer_days is 0 (aka. false), you've got:
switch(false) {
case (false):
$newprice_beer = $beer_price*1;
break;
...
So of course the first case is the one that fires.
One solution would be to test for $beer_days==0 first.
Another would be to use switch($beer_days!==false) instead.
Or rearrange your cases (this also illustrates how switch statements are supposed to be used):
switch($beer_days) {
case 0: $newprice_beer = $beer_price*1.4;
break;
case 10: $newprice_beer = $beer_price*1.2;
break;
default: $newprice_beer = $beer_price*1;
}
You don't say what happens if the number of days is 7 or 14. Are you sure you've thought this through?