Stacking Multiple Ternary Operators in PHP

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

    I understand this is a question about PHP, but since this is just an educational exercise anyways I thought you might be interested in learning that Ruby and Javascript actually behave the way you expect.

    Ruby:

    ree-1.8.7-2012.02 :009 > def foo x
    ree-1.8.7-2012.02 :010?>   x == 1 ? "city 1" : x == 2 ? "city 2" : "out of borders"
    ree-1.8.7-2012.02 :011?>   end
     => nil
    ree-1.8.7-2012.02 :012 > foo 1
     => "city 1"
    ree-1.8.7-2012.02 :013 > foo 2
     => "city 2"
    ree-1.8.7-2012.02 :014 > foo 3
     => "out of borders"
    

    Javascript:

    > function f(x) { return x == 1 ? "city 1" : x == 2 ? "city 2" : "out of borders"; }
    undefined
    > f(1)
    "city 1"
    > f(2)
    "city 2"
    > f(3)
    "out of borders"
    

提交回复
热议问题