You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP代码无法运行报错求助:Function name must be a string

解决PHP报错:Function name must be a string

嘿,这个问题很典型,一眼就能定位到问题所在!你犯了一个PHP数组访问的基础语法错误:

错误原因

$this->cart->contents()返回的是数组集合,每个$items都是一个数组元素。但你在代码里用了**圆括号()**来访问数组的键(比如$items('rowid')),这会让PHP误以为你在调用一个名为$items的函数——可$items是数组类型,不是字符串类型的函数名,所以就抛出了"Function name must be a string"这个报错。

在PHP里,访问数组元素的正确语法是用方括号[],而不是圆括号。

修正后的完整代码

<?php $i = 1; ?> 
<?php foreach ($this->cart->contents() as $items): ?> 
    <input type="hidden" name="<?php echo $i. '(rowid)'; ?>" value="<?php echo $items['rowid']; ?>" /> 
    <tr> 
        <td><input type="text" name="<?php echo $i.'(qty)'; ?>" value="<?php echo $items['qty']; ?>" maxlength="3" /> </td> 
        <td><?php echo $items['name']; ?></td> 
        <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td> 
    </tr> 
<?php $i++; ?> 
<?php endforeach; ?> 
<tr> 
    <td></td> 
    <td class="right"><strong>Total</strong></td> 
    <td class="right" style="text-align:right">$<?php echo $this->cart->format_number($this->cart->total()); ?> </td> 
</tr>

额外注意的细节

我还帮你把$items(price)改成了$items['price']——这里的price是数组的字符串键,一定要加上引号,不然PHP会先尝试把它当作常量解析,虽然某些情况下能正常运行,但这是不规范的写法,很容易引发其他隐藏问题。

以后记得区分数组访问([])和函数调用(())的语法,就能避免这类低级错误啦!

内容的提问来源于stack exchange,提问作者Recency

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:48:41