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:37

It's the 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.

查看更多
人间绝色
3楼-- · 2018-12-31 20:43

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' : '').'.';
查看更多
心情的温度
4楼-- · 2018-12-31 20:44

I think you used the brackets the wrong way. Try this:

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

I think it should work, you can also use:

echo (empty($address['street2']) ? 'Yes <br />' : 'No <br />');
查看更多
时光乱了年华
5楼-- · 2018-12-31 20:45

Conditional Welcome Message

echo 'Welcome '.($user['is_logged_in'] ? $user['first_name'] : 'Guest').'!';

Nested PHP Shorthand

echo 'Your score is:  '.($score > 10 ? ($age > 10 ? 'Average' : 'Exceptional') : ($age > 10 ? 'Horrible' : 'Average') );
查看更多
时光乱了年华
6楼-- · 2018-12-31 20:46

PHP 7+

As of PHP 7, this task can be performed simply by using the Null coalescing operator like this :

echo !empty($address['street2']) ?? 'Empty';

查看更多
步步皆殇っ
7楼-- · 2018-12-31 20:46

Note that when using nested conditional operators, you may want to use parenthesis to avoid possible issues!

It looks like PHP doesn't work the same way as at least Javascript or C#.

$score = 15;
$age = 5;

// The following will return "Exceptional"
echo 'Your score is: ' . ($score > 10 ? ($age > 10 ? 'Average' : 'Exceptional') : ($age > 10 ? 'Horrible' : 'Average'));

// The following will return "Horrible"
echo 'Your score is: ' . ($score > 10 ? $age > 10 ? 'Average' : 'Exceptional' : $age > 10 ? 'Horrible' : 'Average');

The same code in Javascript and C# return "Exceptional" in both cases.

In the 2nd case, what PHP does is (or at least that's what I understand):

  1. is $score > 10? yes
  2. is $age > 10? no, so the current $age > 10 ? 'Average' : 'Exceptional' returns 'Exceptional'
  3. then, instead of just stopping the whole statement and returning 'Exceptional', it continues evaluating the next statement
  4. the next statement becomes 'Exceptional' ? 'Horrible' : 'Average' which returns 'Horrible', as 'Exceptional' is truthy

From the documentation: http://php.net/manual/en/language.operators.comparison.php

It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious.

查看更多
登录 后发表回答