三元运算符和字符串串联怪癖? [英] Ternary operator and string concatenation quirk?
问题描述
我只是想知道为什么这段代码(至少对我来说)产生错误的结果.
Hi I just want to know why this code yields (at least for me) an incorrect result.
好吧,可能我在这里错了
Well, probably i'm in fault here
$description = 'Paper: ' . ($paperType == 'bond') ? 'Bond' : 'Other';
我猜想,如果paperType等于'Bond',则说明为'Paper:Bond',如果paperType不等于'Bond',则说明为'Paper:Other'.
I was guessing that if paperType equals 'Bond' then description is 'Paper: Bond' and if paperType is not equals to 'Bond' then description is 'Paper: Other'.
但是当我运行这段代码时,结果描述是'Bond'或'Other'并留给我 想知道字符串'Paper:'到哪里了?
But when I run this code the results are description is either 'Bond' or 'Other' and left me wondering where the string 'Paper: ' went???
推荐答案
$description = 'Paper: ' . ($paperType == 'bond' ? 'Bond' : 'Other');
尝试添加括号,以便按正确的顺序将字符串连接到字符串.
Try adding parentheses so the string is concatenated to a string in the right order.
这篇关于三元运算符和字符串串联怪癖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!