'switch case for specific conditions

How should one do a switch case of following conditions:

Case 1 would look like this for instance:

if($this->hasA() && $this->hasB() && $this->hasC() && $this->hasD())
{
  # ....
}

Case 2 could look like this and so on:

if($this->hasA() && $this->hasB())
{
  # ....
}

The functions return a boolean value.

This may be not a good practice to do, but I would like to know how this would look like in a switch case.



Solution 1:[1]

I personally wouldn't do this over the if, but you can switch on true:

switch(true) {
    case $this->hasA() && $this->hasB() && $this->hasC() && $this->hasD():
        //code
        break;

    case $this->hasA() && $this->hasB():
        //code
        break;
}

Keep in mind that the functions are executed for each case ($this->hasA() and $this->hasB() twice in above code), so if they are expensive (complex queries, file loads, etc.) then you are better off running them once and then checking the result multiple times.

If any of the cases share some code then you would structure it in order and not use break so that one case would execute through to the next. It's not clear from your example if some have common code.

A simple example:

switch(true) {
    case $this->hasA():
        //code

    case $this->hasB():
        //case above may or may not execute
        //more code

    case $this->hasC():
        //one or both cases above may or may not execute
        //more code
        break;
}

Solution 2:[2]

This would be awkward as a switch; if you must do this, I would just set up as a Boolean like this:

switch($this->hasA() && $this->hasB() && $this->hasC() && $this->hasD()) {

    case true:
        //do true function
    break;

    default:
        //do false function
    break;


}

Only a Boolean type arrangement is possible because either all 4 $this->has() conditions are true or if 1 is false, the whole conditional is false.

Solution 3:[3]

To use a switch statement, you need to combine your four boolean variables into one variable, e.g.

<?php

$a = 1;
$b = 0;
$c = 1;
$d = 1;
$str = $a.$b.$c.$d;

switch($str){
case('0000'):
  echo('Case 1: '.$str);
  break;
case('0001'):
  echo('Case 2: '.$str);
  break;
//...
case('1111');
  echo('Case 16: '.$str);
  break;
}


?>

Solution 4:[4]

I doesn't seem like a good practice. Switch is used mainly when you want to check the value of a single variable (most commonly strings and integers) and execute a block depending on its value.

In your case when you have to evaluate combinations of boolean type variables is better to stick to if else.

Solution 5:[5]

switch case is used in cases where statements are like x=1, not x<1.

Pseudocode:

if (x = 1) {
/*code*/
}

/*vs*/

switch (x) {
    case 1 {
        /*code*/
    }
}

/*and for the x<1 value,*/

if (x < 1) {
    /* code */
}
switch(True) {
    case (x < 1) {
        /* code */
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 The One and Only ChemistryBlob
Solution 3 Craig
Solution 4 iliaz
Solution 5 Prakhar Vatsa