Stacking Multiple Ternary Operators in PHP

后端 未结 8 2168
忘了有多久
忘了有多久 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:10

    Don't abuse the ternary operator for that sort of thing. It makes debugging near impossible to follow. Why not do something like

    switch($province) {
        case 6: $Myprovince = "city-1"; break;
        case 7: ...
    }
    

    or simply some chained if/then/else

    if ($province == 6) {
         $Myprovince = "city-1";
    } elseif ($province = ...) {
       ...
    }
    

提交回复
热议问题