我正在为学校做一个PHP脚本,我对这些行有错误。
// calculate total
$total_price = $crystal_price * $item_numbers + $postage_price;
if ($coupon_code=="FREEPOST" && $total_price >= 1000 ) $postage_price = 0 ; && $total_cost = $item_numbers * $crystal_price
else if ($coupon_code=="HALFPOST" && $total_price >= 400 ) $postage_price = $postage_price * 0.5 ; && $total_cost = $item_numbers * $crystal_price + $postage_cost * 0.5 ;
如果删除if
语句的第二部分,它可以正常工作,但是我需要它,有什么方法可以解决这个问题吗?
答案 0 :(得分:1)
它位于第二行&& $total_cost = $item_numbers * $crystal_price
。那些&&
在那里不合适。
如果您稍微调整代码,这会有所帮助:
// calculate total
$total_price = $crystal_price * $item_numbers + $postage_price;
if ($coupon_code=="FREEPOST" && $total_price >= 1000 )
$postage_price = 0 ;
&& $total_cost = $item_numbers * $crystal_price
else if ($coupon_code=="HALFPOST" && $total_price >= 400 )
$postage_price = $postage_price * 0.5 ;
&& $total_cost = $item_numbers * $crystal_price + $postage_cost * 0.5 ;
如果这样做,您可以更轻松地发现错误。看看上面的代码,我想你也忘记了一些大括号:
// calculate total
$total_price = $crystal_price * $item_numbers + $postage_price;
if ($coupon_code == "FREEPOST" && $total_price >= 1000)
{
$postage_price = 0;
$total_cost = $item_numbers * $crystal_price;
}
else if ($coupon_code == "HALFPOST" && $total_price >= 400)
{
$postage_price = $postage_price * 0.5;
$total_cost = $item_numbers * $crystal_price + $postage_cost * 0.5;
}