Php 当购物车为空时尝试显示消息
我使用数组创建了一个购物车。当数组为空时,我试图输出一条“您的购物车为空”消息。当我尝试这个时,它似乎对我不起作用Php 当购物车为空时尝试显示消息,php,sql,arrays,if-statement,Php,Sql,Arrays,If Statement,我使用数组创建了一个购物车。当数组为空时,我试图输出一条“您的购物车为空”消息。当我尝试这个时,它似乎对我不起作用 <?php if (isset($_SESSION['shopping_cart'])): if (count($_SESSION['shopping_cart']) < 1): ?> <a>Your cart is empty.</a>
<?php
if (isset($_SESSION['shopping_cart'])):
if (count($_SESSION['shopping_cart']) < 1):
?>
<a>Your cart is empty.</a>
<?php endif; endif; ?>
<?php
if (isset($_SESSION['shopping_cart'])):
if (count($_SESSION['shopping_cart']) > 0):
?>
<a href="checkout.php" class="check-button">Checkout</a>
<?php endif; endif; ?>
使用empty()
而不是isset
<?php if (empty($_SESSION['shopping_cart'])): ?>
<a>Your cart is empty.</a>
<?php endif; ?>
<?php if ( ! empty($_SESSION['shopping_cart'])):?>
<a href="checkout.php" class="check-button">Checkout</a>
<?php endif; ?>
请回显$\u SESSION['shopping\u cart']
并在此处显示输出我刚刚尝试过,但当数组为空时,我仍然没有收到任何文本输出。尝试var\u dump($\u SESSION['shopping\u cart'])
并将结果发布在此处。如果没有结果,请在初始化$\u会话['shopping\u cart']