【发布时间】:2017-01-04 12:00:00
【问题描述】:
我想简写 if 语句,但我的代码编辑器显示有错误。有什么问题?
我原来的 if 语句
<?php
if($shippingCost["ShippingCost"]['shipping_type'] == 'order_sum') {
echo $this->Format->money($shippingCost["ShippingCost"]['value_to']);
} else {
echo number_format($shippingCost["ShippingCost"]['value_to']);
}
?>
我的简写版本
<?php
($shippingCost["ShippingCost"]['shipping_type'] == 'order_sum') ? //IDE error expected colon
echo $this->Format->money($shippingCost["ShippingCost"]['value_from']) : // IDE error expected semicolon
echo number_format($shippingCost["ShippingCost"]['value_from'])
?>
【问题讨论】:
-
echo a? a : b -
你真的不应该那样写。这看起来很糟糕
-
您可以在手册中阅读它php.net/manual/en/… 但谁看手册。
-
这不是一个有效的缩短代码,如果你想通过它使用 echo ...但它不会像这样工作
标签: php ternary-operator