简体   繁体   中英

How I can make switch case check all cases

What if I want to make each case in the switch case been a equlity condition like this code example

switch (true){
   case X==Y:
    //do something 
   case X==Z:
    //do something 
   case X has 0 :
    //do something 
}

Its working well if I want to check only one condition,By adding break ,But what if i want to check all cases and do only true cases

If you want to change flavor from if.....else and want to use switch....case

So you can try like this

$test = true;
$x=1;$y=1;$z=2;//uncomment any one and check
//$x=1;$y=2;$z=1;//uncomment any one and check
//$x=0;$y=1;$z=1;//uncomment any one and check
switch ($test){
   case $x==$y:
        echo "x==y";
        break;
   case $x==$z:
        echo "x==z";
        break;
   case $x==0 :
        echo "x has 0";
        break;
   default:
        echo "he!he!he!he!he!he!";
}

uncomment any one and check all conditions

Check demo here : https://eval.in/907483

Here using $test = true; means if your condition goes true in case inside switch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM