Stacking Multiple Ternary Operators in PHP

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

    Others have already suggested the right way of doing it but if you really want to use ternary operator you need to use parenthesis as:

    $province = 7;
     $Myprovince = (
     ($province == 6) ? "city-1" :
      (($province == 7) ? "city-2" :
       (($province == 8) ? "city-3" :
        (($province == 30) ? "city-4" : "out of borders")))
     );
    

    Updated Link

提交回复
热议问题