Php 如果语句有错误,那么就用人手不足
我想速记下面的if语句,但我的代码编辑器显示有错误。有什么问题 我的原始if语句Php 如果语句有错误,那么就用人手不足,php,ternary-operator,Php,Ternary Operator,我想速记下面的if语句,但我的代码编辑器显示有错误。有什么问题 我的原始if语句 <?php if($shippingCost["ShippingCost"]['shipping_type'] == 'order_sum') { echo $this->Format->money($shippingCost["ShippingCost"]['value_to']); } else { echo number_format($shippingCost["Shi
<?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没有任何返回类型。这就是为什么您应该在开始时使用print或write echo
一,。在开始时使用echo
二,。使用打印而不是回音
三元运算符是一个通用的速记if语句,它只是返回表达式的一种速记方式。在这种情况下,您可以仅在表达式上使用三元组,并从中提取回显:
echo ($shippingCost["ShippingCost"]['shipping_type'] == 'order_sum') ?
$this->Format->money($shippingCost["ShippingCost"]['value_from']) :
number_format($shippingCost["ShippingCost"]['value_from']);
回音a?你真的不应该那样写。这看起来很糟糕。你可以在手册中阅读,但谁看手册。这不是有效的缩短代码,如果你想查看它,请使用echo。。。但它不会像这样工作
($shippingCost["ShippingCost"]['shipping_type'] == 'order_sum') ?
print $this->Format->money($shippingCost["ShippingCost"]['value_from']) :
print number_format($shippingCost["ShippingCost"]['value_from']);
echo ($shippingCost["ShippingCost"]['shipping_type'] == 'order_sum') ?
$this->Format->money($shippingCost["ShippingCost"]['value_from']) :
number_format($shippingCost["ShippingCost"]['value_from']);