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

CodeIgniter3中使用number_format格式化价格时遇语法错误

解决CodeIgniter3中价格格式化的语法错误问题

你的代码错误根源在于number_format函数的括号嵌套错误,以及str_replace的参数位置混乱,同时还遗漏了将数据库整数价格转换为实际金额的关键步骤(除以100)。

正确代码实现

$template = str_replace('{price}', number_format($row['price'] / 100, 2, '.', ''), $template);

错误修正说明

  • 先将数据库存储的整数价格(如42277)除以100,得到实际欧元金额(422.77)
  • 修正number_format的调用格式:参数顺序为「待格式化数字、小数位数、小数点分隔符、千位分隔符」,原代码错误地给第一个参数套了多余括号,导致语法报错
  • 把$template放回str_replace的第三个参数位置,原代码错误地将其放到了number_format的括号内

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 20:46:00