Home » Php » PHP – Switch multiple case statement

PHP – Switch multiple case statement

Posted by: admin November 30, 2017 Leave a comment

Questions:

Can someone suggest me how can I replace the below code
How to rewrite the code in order to avoid the repeatation of the block case 3:{code block A; break;}

`

switch(i){
  case 1:{code block A; break;}
  case 2:{code block b; break;}
  case 3:{code block A; break;}
  default: {code block default; break;}
}

`

How to have combined code for case 1 & case 3

Answers:
switch (i) {
    case 1:
    case 3:
        code block A;
        break;
    case 2:
        code block B;
        break;
    default:
        code block default;
        break;
}

Questions:
Answers:

Something like this

switch(i){
    case 1:
    case 3:{code block A; break;}
    case 2:{code block b; break;}
    default: {code block default; break;}
}