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

PHP number_format函数报错:参数2需为int,求问题排查

问题原因与修复方案

你的代码报错核心是函数调用语法错误,误用了按位或运算符|来传递参数给number_format,完全不符合PHP的函数调用规则:

错误根源

你写的(($regular_price - $total))|number_format (1, '.', ',')会被PHP解析为:

  1. 先执行number_format(1, '.', ',')——这里第二个参数传了字符串.,但number_format要求第二个参数(小数位数)必须是整数,直接触发警告
  2. 再把($regular_price - $total)的结果和上面的函数返回值做按位或运算,完全偏离了你想要格式化计算结果的需求

修正后的代码

<span class="item-value summary">'. number_format(($regular_price - $total), 1, '.', ',') . ' € (' . number_format((($regular_price - $total) / $regular_price) * 100, 1, '.', ',') . ' %)</span>

关键修正点

  • 将折扣金额($regular_price - $total)、折扣率(($regular_price - $total)/$regular_price)*100作为number_format的第一个参数传入
  • 确保number_format的第二个参数(保留小数位数)是整数1,小数点分隔符、千位分隔符依次作为第三、第四个参数传递
  • 用PHP的字符串连接符.替代错误的|,正确拼接HTML片段和格式化后的数值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 19:46:17