使用相同的货币"egp"获得 2 个不同的输出
$currency = ($q->currency == 'egp')? '£' : (($q->currency == 'usd') ? '$' : '€');
此行输出$
$currency = ($q->currency == 'egp')? '£' : ($q->currency == 'usd') ? '$' : '€';
这个输出£
我找不到为什么?
注意:唯一的区别是第二个三元运算符语句周围的()
请考虑以下代码:
echo (true?"Left is first":(true?"Right is first":""));
左边是第一个
对
echo (true?"Left is first":true?"Right is first":"");
解释右是第一位的
可以在 http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary 找到。
简而言之,在第二种情况下,PHP 将评估true?"Left is first":true
作为三元表达式的条件。这将计算为 Left is first
,评估结果为 true,因此Right is first
将被回显