Stacking Multiple Ternary Operators in PHP

后端 未结 8 2325
忘了有多久
忘了有多久 2020-11-22 03:49

This is what I wrote :

 $Myprovince = (
($province == 6) ? \"city-1\" :
($province == 7) ? \"city-2\" :
($province == 8) ? \"city-3\" :
($province == 30) ? \         


        
8条回答
  •  野性不改
    2020-11-22 04:16

    Use switch instead. Ternary operators really shouldn't be used for more than single conditions, as they quickly become very difficult to understand.

    switch ($province) {
        case 6:
            $Myprovince = 'city-1';
            break;
        case 7:
            $Myprovince = 'city-2';
            break;
        case 8:
            $Myprovince = 'city-3';
            break;
        case 30:
            $Myprovince = 'city-4';
            break;
        default:
            $Myprovince = 'out of borders';
    }
    

提交回复
热议问题