How do I use the ternary operator ( ? : ) in PHP a

2018-12-31 20:31发布

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 works like a charm!

echo (empty($storeData['street2'])) ? 'Yes <br />' : 'No <br />';

11条回答
闭嘴吧你
2楼-- · 2018-12-31 20:51

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';
查看更多
冷夜・残月
3楼-- · 2018-12-31 20:52

There's also a shorthand ternary operator and it looks like this:

(expression1) ?: expression2 will return expression1 if it evaluates to true or expression2 otherwise.

Example:

$a = 'Apples';
echo ($a ?: 'Oranges') . ' are great!';

will return

Apples are great!

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

From the PHP Manual

查看更多
还给你的自由
4楼-- · 2018-12-31 20:55

You can do this even shorter by replacing echo with <?= code ?>

<?=(empty($storeData['street2'])) ? 'Yes <br />' : 'No <br />'?>

This is useful especially when you want to determine, inside a navbar, whether the menu option should be displayed as already visited (clicked) or not:

<li<?=($basename=='index.php' ? ' class="active"' : '')?>><a href="index.php">Home</a></li>

查看更多
美炸的是我
5楼-- · 2018-12-31 20:58

Here are some interesting examples, with one or more varied conditions.

$value1 = '1';
$value2 = '2';
$value3 = '3';

// 1 Condition
$v1 = ($value1 == '1') ? TRUE : FALSE;
var_dump($v1);
echo "<br>";

// 2 Conditions
$v2 = ($value1 == '' ? TRUE : ($value2 == '2' ? TRUE : FALSE));
var_dump($v2);
echo "<br>";

// 3 Conditions
$v3 = ($value1 == '' ? TRUE : ($value2 == '' ? TRUE : ($value3 == '3' ? TRUE : FALSE)));
var_dump($v3);
echo "<br>";

// 4 Conditions
$v4 = ($value1 == '1') ? ($value2 == '2' ? ($value3 == '3' ? TRUE : 'FALSE V3') : 'FALSE V2') : 'FALSE V1' ;
var_dump($v4);
echo "<br>";
查看更多
宁负流年不负卿
6楼-- · 2018-12-31 21:02

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.

查看更多
登录 后发表回答