<分区>
我们知道,如果我们使用 javascript 或 python,我们可以使用以下语句来获取变量或其默认值。
// javascript
alert(a || 'default');
# python
print(a or 'default');
在 php 中,我们可能需要调用以下内容:
echo $a ? $a : 'default';
如果 $a 是一个很长的语句,情况就更糟了:
echo (
we_made_many_calculation_here_and_this_is_the_result() ?
we_made_many_calculation_here_and_this_is_the_result() :
'default'
);
或
var $result = we_made_many_calculation_here_and_this_is_the_result();
echo $result ? $result : 'default';
以上任何一项,我认为都不整洁。
而且我盲目地寻找任何答案,寻找语句或内置函数来简化工作。但是我尝试了很多方法都找不到答案。
所以请帮忙。