我在下面写的代码有问题。基本上,当购物车中有 x 件商品时,它会回显文本“您在购物车中有 x 件商品”。但是,当没有商品时,它应该回显“您在购物车中没有任何商品”,而是什么也不回显。我究竟做错了什么?
<?php
$array = unserialize($_SESSION['__vm']['vmcart']);
foreach($array->products as $product){
$amount = $product->amount;
if ($amount != 0){ echo "You have $amount item(s) in the cart."; }
else { echo "You don't have any items in the cart."; }
}
?>