utdev
utdev

Reputation: 4112

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.

Upvotes: 1

Views: 160

Answers (5)

Prakhar Vatsa
Prakhar Vatsa

Reputation: 73

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 */
    }
}

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 79024

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

Upvotes: 1

iliaz
iliaz

Reputation: 395

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.

Upvotes: 0

Craig
Craig

Reputation: 444

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


?>

Upvotes: 0

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.

Upvotes: -1

Related Questions