How do I use the ternary operator ( ? : ) in PHP as a shorthand for “if / else”?

后端 未结 14 1262
我在风中等你
我在风中等你 2020-11-22 08:34

Based on the examples from this page, I have the working and non-working code samples below.

Working code using if statement:

if (!empty         


        
14条回答
  •  孤独总比滥情好
    2020-11-22 09:07

    It's the Ternary operator a.k.a Elvis operator (google it :P) you are looking for.

    echo $address['street2'] ?: 'Empty'; 
    

    It returns the value of the variable or default if the variable is empty.

提交回复
热议问题