代码之家  ›  专栏  ›  技术社区  ›  ptrcao

在WordPress[duplicate]中调用PHP函数中的自定义字段

  •  -1
  • ptrcao  · 技术社区  · 6 年前

    实现下面第二个功能的正确方法是什么?第一个函数可以正常工作,但第二个函数不能正确调用WordPress中键为的自定义字段 _ni_cost_goods .

    分析错误:语法错误,意外的“=”,应为变量

    <?php
    function round_price( $price = null, $multiplier = 1, $nearest = 1, $minus = 0 ) {
        if ( !empty( $price ) ) {
            // strip any extra characters from price
            $price = preg_replace("/[^0-9,.]/", "", $price);
            // perform calculations
            return ( round ( ( $price * $multiplier ) / $nearest ) * $nearest ) - $minus; 
        }
    }
    
    function add_gst( _ni_cost_goods = null, $multiplier = 1.1) {
        if ( !empty( _ni_cost_goods ) ) {
            // strip any extra characters from price
            _ni_cost_goods = preg_replace("/[^0-9,.]/", "", _ni_cost_goods);
            // perform calculations
            return _ni_cost_goods * $multiplier; 
        }
    }
    ?>
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Adam    6 年前

    function add_gst( $_ni_cost_goods = null, $multiplier = 1.1) {
    
        if ( !empty( $_ni_cost_goods ) ) {
    
            // strip any extra characters from price
            $_ni_cost_goods = preg_replace("/[^0-9,.]/", "", $price);
    
            // perform calculations
            return $_ni_cost_goods * $multiplier; 
        }
    }
    

    $ 在多个地方签署“NIU cost”商品。也就是说,你也在访问一个变量 $price 它不在函数的范围内。