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

时间:2018-10-08 14:17:31

标签: php wordpress function

实现下面第二个功能的正确方法是什么?第一个功能运行良好,但是第二个功能无法正确调用关键字为_ni_cost_goods的WordPress中的自定义字段。

  

解析错误:语法错误,意外的'=',期望变量   (T_VARIABLE)在第11行的代码中

<?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 个答案:

答案 0 :(得分:0)

在第二个函数中,您遇到一些问题,将变量传递给函数并返回。

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; 
    }
}

您会注意到我在_ni_cost_goods的多个位置添加了$符号。话虽这么说,您还可以访问变量$price,该变量不在该函数的范围内。