简体   繁体   English

count():参数必须是一个数组或一个 object 实现 php 中的可计数错误

[英]count(): Parameter must be an array or an object that implements Countable error in php

I am creating an add-to-cart system in my website.我正在我的网站上创建一个添加到购物车的系统。

If there are no items in the cart, the variable $cart is set to NULL .如果购物车中没有商品,则变量$cart设置为NULL

This results in the following error when I try to echo the number of items in the cart: count(): Parameter must be an array or an object that implements Countable .当我尝试回显购物车中的商品数量时,这会导致以下错误: count(): Parameter must be an array or an object that implements Countable

Here's my php code:这是我的 php 代码:

<?php 
if ((isset($_SESSION['active_user_type']) && $_SESSION['active_user_type'] == "consumer") || !isset($_SESSION['active_user'])) {
?>
<div class="shopping_cart">
<div class="cart_title">
    <a href="view_cart.php">Shopping cart</a>
</div>

<?php
    $total = 0;
    if(isset($_SESSION['cart'])) {
        $cart = $_SESSION['cart'];
        for ($i=0; $i<count($cart); $i++) {
            $item_id = $cart[$i][0];
            $query = "SELECT * FROM items WHERE id=$item_id";
            $result = $db->query($query);
            if ($row = $result->fetch()) {
                $price = ($row['price']*$cart[$i][1]) + $row['shipping_price'];
            }
            $total += $price;
        }
    } else {
        $cart = NULL;
    }
?>
<div class="cart_details">
// the error seems to be from the line below:
    <?php echo count($cart); ?><br />
    <span class="border_cart"></span> Total: 
    <span class="price">
        <?php echo "BD " . number_format((float)$total,3,'.',''); ?>
    </span>
</div>
<div class="cart_icon">
    <a href="checkout.php" title="Checkout">
        <img src="images/shoppingcart.png" alt="" width="48" height="48" border="0" />
    </a>
</div>
</div>
<?php
}
?>

This is because you are counting on 'NULL', Try it like this:这是因为你指望'NULL',试试这样:

.
.
.
else
{
    $cart = [];
}

When $cart == NULL (as it is assigned in your else clause) it is not count able since NULL does not have a Countable interface.$cart == NULL (因为它在你的else子句中分配)时,它是count的,因为NULL没有Countable接口。 As of PHP 7.2 , this results in the warning message you have seen.PHP 7.2 开始,这会导致您看到警告消息。

But it seems you shouldn't try to output a cart when you don't have one, so you should just move that code inside your if block ie但是,当您没有购物车时,您似乎不应该尝试 output 购物车,因此您应该将该代码移到if块中,即

$total = 0;
if(isset($_SESSION['cart']))
{
    $cart = $_SESSION['cart'];
    for ($i=0; $i<count($cart); $i++)
    {
        $item_id = $cart[$i][0];
        $query = "SELECT * FROM items WHERE id=$item_id";
        $result = $db->query($query);
        if ($row = $result->fetch())
        {
            $price = ($row['price']*$cart[$i][1]) + $row['shipping_price'];
        }
        $total += $price;
    }
?>
<div class="cart_details"> <?php echo count($cart);?> <br />
    <span class="border_cart"></span> Total: <span class="price"><?php echo "BD " . 
  number_format((float)$total,3,'.',''); ?></span> </div>
<div class="cart_icon"><a href="checkout.php" title="Checkout"><img src="images/shoppingcart.png" 
alt="" width="48" height="48" border="0" /></a></div>
 </div>
<?php
}
else
{
    $cart = NULL;
}
?>

It's because you're trying to count something that's not countable (how can you count the number of elements in null?).这是因为您试图计算不可数的东西(如何计算 null 中的元素数量?)。 From the documentation:文档中:

7.2.0: count() will now yield a warning on invalid countable types passed to the array_or_countable parameter. 7.2.0:count() 现在将对传递给array_or_countable参数的无效可数类型产生警告。

so prior to version 7.2.0 this warning wouldn't be emitted.所以在版本 7.2.0 之前不会发出此警告。 In all versions, if obj in count(obj) isn't a valid array/countable object the function returns 1 , with the exception of count(null) which returns 0 .在所有版本中,如果count(obj) obj的 obj 不是有效的数组/可计数 object ,则 function 返回1 ,但count(null)除外,它返回0


You can either:您可以:

  1. Cast it to an array将其转换为数组
  2. Explicitly set it to an empty array if it's empty如果为空,则将其显式设置为空数组
  3. Put a check in place before echoing在回显之前进行检查

1: <?php echo count((array)$cart);?> 1: <?php echo count((array)$cart);?>

2: else { $cart = []; } 2: else { $cart = []; } else { $cart = []; }

3: <?php ($cart == null)? '': echo count($cart);?> 3: <?php ($cart == null)? '': echo count($cart);?> <?php ($cart == null)? '': echo count($cart);?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP 错误 - 警告:count():参数必须是实现 Countable 的数组或对象 - PHP Error - Warning: count(): Parameter must be an array or an object that implements Countable PHP错误:警告:count():参数必须是实现Countable的数组或对象 - PHP error: Warning: count(): Parameter must be an array or an object that implements Countable PHP警告:count():参数必须是数组或实现Countable的对象? - PHP Warning: count(): Parameter must be an array or an object that implements Countable? PHP 7.2: count(): 参数必须是数组或实现Countable的object - PHP 7.2: count(): Parameter must be an array or an object that implements Countable PHP 7.2 警告count():参数必须是数组或实现Countable的object - PHP 7.2 Warning count(): Parameter must be an array or an object that implements Countable PHP count():参数必须是一个数组或一个实现了Countable的对象 - PHP count(): Parameter must be an array or an object that implements Countable PHP:count():参数必须是数组或者实现了Countable的对象 - PHP: count(): Parameter must be an array or an object that implements Countable PHP 出现问题“消息:count():参数必须是数组或实现可计数的 object” - Problem with PHP “Message: count(): Parameter must be an array or an object that implements Countable ” count():参数必须是在其中实现Countable的数组或对象 - count(): Parameter must be an array or an object that implements Countable in phpdoc:PHP警告:count():参数必须是实现Countable的数组或对象 - phpdoc: PHP Warning: count(): Parameter must be an array or an object that implements Countable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM