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

WP CLI PHP脚本触发‘格式不正确的数值’通知错误,寻求修复方法

PHP Notice: A non well formed numeric value encountered 错误排查与修复

问题说明

脚本运行时触发PHP Notice: A non well formed numeric value encountered通知,报错行是价格计算逻辑:

$rounded_price = ( ceil ( ( $price * $multiplier ) / $nearest ) * $nearest ) - $minus;

完整脚本如下:

function round_price_with_limits( $price = null, $multiplier = 9.3, $nearest = 10, $minus = 0.05, $map = 0, $msrp = 9999999999) {
        // Ensure a price was provided.
        if ( !empty( $price ) ) {
            // Calculate price with markup and round it.
            $rounded_price = ( ceil ( ( $price * $multiplier ) / $nearest ) * $nearest ) - $minus; 
            // If price is less than minimum, return minimum.
            if($rounded_price < $map){
                return $map;
            }
            // If price is greater than maximum, return maximum.
            elseif($rounded_price > $msrp){
                return $msrp;
            } else {
                // Return price otherwise.
                return $rounded_price;
            }
        }
    }

错误原因

这个通知的本质是参与计算的变量不是有效数值类型:

  • 比如$price传入了带非数字字符的字符串(如"199元"、"abc")、空字符串,或者其他无法被PHP解析为有效数值的类型;
  • 原代码用!empty($price)判断,会把空字符串、"0"这类无效计算值放行,导致PHP在隐式类型转换时触发通知。

修复方案

方案1:强制转换参数为数值类型

在计算前将所有参与运算的参数转为浮点型,确保计算时都是有效数值:

function round_price_with_limits( $price = null, $multiplier = 9.3, $nearest = 10, $minus = 0.05, $map = 0, $msrp = 9999999999) {
        // 转换并校验所有参数为数值类型
        $price = is_numeric($price) ? (float)$price : null;
        $multiplier = (float)$multiplier;
        $nearest = (float)$nearest;
        $minus = (float)$minus;
        $map = (float)$map;
        $msrp = (float)$msrp;

        // 仅允许有效正数值进入计算
        if ( !is_null($price) && $price > 0 ) {
            $rounded_price = ( ceil ( ( $price * $multiplier ) / $nearest ) * $nearest ) - $minus; 
            
            if($rounded_price < $map){
                return $map;
            } elseif($rounded_price > $msrp){
                return $msrp;
            } else {
                return $rounded_price;
            }
        }
        // 处理无效price的情况,返回最低限价或null(按需调整)
        return $map;
    }

方案2:严格校验输入参数

在函数开头添加参数校验,直接拦截无效值,避免进入计算逻辑:

function round_price_with_limits( $price = null, $multiplier = 9.3, $nearest = 10, $minus = 0.05, $map = 0, $msrp = 9999999999) {
        // 严格校验$price必须是有效正数值
        if ( !is_numeric($price) || $price <= 0 ) {
            return $map; // 或返回null,根据业务需求调整
        }

        // 转换其他参数为数值类型
        $multiplier = (float)$multiplier;
        $nearest = (float)$nearest;
        $minus = (float)$minus;
        $map = (float)$map;
        $msrp = (float)$msrp;

        $rounded_price = ( ceil ( ( $price * $multiplier ) / $nearest ) * $nearest ) - $minus; 
        
        if($rounded_price < $map){
            return $map;
        } elseif($rounded_price > $msrp){
            return $msrp;
        } else {
            return $rounded_price;
        }
    }

方案3:修复空值判断逻辑

原代码的!empty($price)判断不严谨,替换为更精准的数值校验:
将if ( !empty( $price ) )改为if ( is_numeric($price) && $price > 0 ),确保只有有效正数值才进入计算流程。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 06:50:27