这两行有什么区别


what is the difference between these two lines

使用相同的货币"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将被回显