ecshop number_format() 在php5.3以上的错误解决方案

本文介绍了解决ECShop在PHP5.3以上版本中出现的兼容性问题,特别是针对number_format函数处理空字符串值引发的错误。通过修改价格格式化函数price_format,确保在不同配置下都能正确显示商品价格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Warning: number_format() expects parameter 1 to be double, string given inD:\*******\includes\lib_common.php on line 959

Warning: number_format() expects parameter 1 to be double, string given inD:\*******\includes\lib_common.php on line 959



这个错误是ecshop 在php5.3以上的报错


原因是配送插件里面的免费额度为0,ec本身的bug导致了$price的值为空值,直接调用number_format出现了错误。


解决方案如下:

/**
 * 格式化商品价格
 *
 * @access  public
 * @param   float   $price  商品价格
 * @return  string
 */
function price_format($price, $change_price = true)
{
   
    if ($change_price && defined('ECS_ADMIN') === false)
    {
        switch ($GLOBALS['_CFG']['price_format'])
        {
            case 0:
                $price = number_format($price, 2, '.', '');
                break;
            case 1: // 保留不为 0 的尾数
                $price = preg_replace('/(.*)(\\.)([0-9]*?)0+$/', '\1\2\3', number_format($price, 2, '.', ''));


                if (substr($price, -1) == '.')
                {
                    $price = substr($price, 0, -1);
                }
                break;
            case 2: // 不四舍五入,保留1位
                $price = substr(number_format($price, 2, '.', ''), 0, -1);
                break;
            case 3: // 直接取整
                $price = intval($price);
                break;
            case 4: // 四舍五入,保留 1 位
                $price = number_format($price, 1, '.', '');
                break;
            case 5: // 先四舍五入,不保留小数
                $price = round($price);
                break;
        }
    }
    else
    {

        if(!$price){
                $price = 0;
        }

       
        $price = number_format($price, 2, '.', '');
    }


    return sprintf($GLOBALS['_CFG']['currency_format'], $price);
}


      


 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值