The conditional operator – also known as the ternary operator – is an alternative form of the if/else statement that helps you to write conditional code blocks in a more concise way. First, you need to write a conditional expression that evaluates into either true or false .
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.
Example : C++ Ternary OperatorSuppose the user enters 80. Then, the condition marks >= 40 evaluates to true . Hence, the first expression "passed" is assigned to result . Enter your marks: 39.5 You failed the exam.
In C++, the ternary operator is used to replace a multiline if-else code with a single line. It's a shorthand if-else statement as it contains three operands.
The
(condition) ? /* value to return if condition is true */
: /* value to return if condition is false */ ;
syntax is not a "shorthand if" operator (the ?
is called the conditional operator) because you cannot execute code in the same manner as if you did:
if (condition) {
/* condition is true, do something like echo */
}
else {
/* condition is false, do something else */
}
In your example, you are executing the echo
statement when the $address
is not empty. You can't do this the same way with the conditional operator. What you can do however, is echo
the result of the conditional operator:
echo empty($address['street2']) ? "Street2 is empty!" : $address['street2'];
and this will display "Street is empty!" if it is empty, otherwise it will display the street2 address.
As of PHP 7, this task can be performed simply by using the Null coalescing operator like this :
echo $address['street2'] ?? 'Empty';
Basic True / False Declaration
$is_admin = ($user['permissions'] == 'admin' ? true : false);
Conditional Welcome Message
echo 'Welcome '.($user['is_logged_in'] ? $user['first_name'] : 'Guest').'!';
Conditional Items Message
echo 'Your cart contains '.$num_items.' item'.($num_items != 1 ? 's' : '').'.';
ref: https://davidwalsh.name/php-ternary-examples
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.
The ternary operator is just a shorthand for and if/else block. Your working code does not have an else condition, so is not suitable for this.
The following example will work:
echo empty($address['street2']) ? 'empty' : 'not empty';
Quick and short way:
echo $address['street2'] ? : "No";
Here are some interesting examples, with one or more varied conditions.
$color = "blue";
// Example #1 Show color without specifying variable
echo $color ? : "Undefined";
echo "<br>";
// Example #2
echo $color ? $color : "Undefined";
echo "<br>";
// Example #3
echo ($color) ? $color : "Undefined";
echo "<br>";
// Example #4
echo ($color == "blue") ? $color : "Undefined";
echo "<br>";
// Example #5
echo ($color == "" ? $color : ($color == "blue" ? $color : "Undefined"));
echo "<br>";
// Example #6
echo ($color == "blue" ? $color : ($color == "" ? $color : ($color == "" ? $color : "Undefined")));
echo "<br>";
// Example #7
echo ($color != "") ? ($color != "" ? ($color == "blue" ? $color : "Undefined") : "Undefined") : "Undefined";
echo "<br>";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With