How do I use the ternary operator ( ? : ) in PHP as a shorthand for “if / else”?
问题 Based on the examples from this page, I have the working and non-working code samples below. Working code using if statement: if (!empty($address[\'street2\'])) echo $address[\'street2\'].\'<br />\'; Non-working code using ternary operator: $test = (empty($address[\'street2\'])) ? \'Yes <br />\' : \'No <br />\'; // Also tested this (empty($address[\'street2\'])) ? \'Yes <br />\' : \'No <br />\'; UPDATE After Brian\'s tip, I found that echoing $test outputs the expected result. The following