Based on the examples from this page, I have the working and non-working code samples below.
Working code using if
statement:
if (!empty
Quick and short way:
echo $address['street2'] ? : "No";
Here are some interesting examples, with one or more varied conditions.
$color = "blue";
// Condition #1 Show color without specifying variable
echo $color ? : "Undefined";
echo "
";
// Condition #2
echo $color ? $color : "Undefined";
echo "
";
// Condition #3
echo ($color) ? $color : "Undefined";
echo "
";
// Condition #4
echo ($color == "blue") ? $color : "Undefined";
echo "
";
// Condition #5
echo ($color == "" ? $color : ($color == "blue" ? $color : "Undefined"));
echo "
";
// Condition #6
echo ($color == "blue" ? $color : ($color == "" ? $color : ($color == "" ? $color : "Undefined")));
echo "
";
// Condition #7
echo ($color != "") ? ($color != "" ? ($color == "blue" ? $color : "Undefined") : "Undefined") : "Undefined";
echo "
";