0

I want to store a certain value during a switch/case in PHP, but I don't see what is wrong with this code:

<?php
$weegfactor=67;
$lus=12;

    if($weegfactor<70):
        switch($lus){
        case(1 || 7):
        $verdeling="AAABCD";
        break;
        case(2 || 8):
        $verdeling="AAABBE";
        break;
        case(3 || 9):
        $verdeling="AAAABC";
        break;
        case(4 || 10):
        $verdeling="AABBBD";
        break;
        case(5 || 11):
        $verdeling="ABBBCC";
        break;
        case(6 || 12):
        $verdeling="AABCCC";
        break;
        }
    endif;  

echo "weegfactor ",$weegfactor . '</br>' ;
echo "lus : ",$lus . '</br>';
echo "verdeling ",$verdeling;

?>

The outcome of the above code is: weegfactor 67 lus : 12 verdeling AAABCD

Which is not correct because $verdeling should be "AABCCC". What is my mistake??

5
  • Hint: what does var_dump(1 || 7) show? Commented Jan 6, 2018 at 20:01
  • It shows nothing: it is meant to determine the value of $verdeling. Commented Jan 6, 2018 at 20:04
  • you shoud use && not || see this link Commented Jan 6, 2018 at 20:05
  • I think you should make one case for every number 1-12. Twelve case: I thiink previous poster is on the right track @Barmar Commented Jan 6, 2018 at 20:08
  • @user3107597 Did you try it? I just did, it printed bool(true). Commented Jan 6, 2018 at 20:09

3 Answers 3

1

1 || 7 evaluates to a boolean type. So your program is doing a boolean comparison of $lus and (1 || 7).

You will need to use two separate case statements for each:

switch($lus){
    case(1):
    case(7):
        $verdeling="AAABCD";
        break;
    case(2):
    case(8):
        $verdeling="AAABBE";
        break;
    case(3):
    case(9):
        $verdeling="AAAABC";
        break;
    case(4):
    case(10):
        $verdeling="AABBBD";
        break;
    case(5):
    case(11):
        $verdeling="ABBBCC";
        break;
    case(6):
    case(12):
        $verdeling="AABCCC";
        break;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Other solutions also did the job, but I like this one because it is for me the most clear solution.
0

With case (x || y) you probably mean when it is x or y. In that case you should do this:

switch($lus) {
   case x:
   case y:
      // do stuff
      break;
}

What you have now is a check against x || y, which is always resolves to a "truish" value, just like your value 12 is "truish", making the switch take the first branch.

Comments

0

The value of 1 || 7 is TRUE, because both 1 and 7 are truthy. So the first case is equivalent to:

case TRUE:

And in fact, all your cases are equivalent to case TRUE:.

So it's then testing $lus == TRUE. When comparing any type to a boolean, the other type is first converted to boolean (see Comparison Operators for the complete list of how loose comparisons of different types are done). Since 12 is truthy (any non-zero integer is truthy), this comparison succeeds.

If you want to test against multiple values, use multiple case statements:

case 1:
case 7:
    $verdeling="AAABCD";
    break;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.