且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将一个函数中创建的PHP变量传递给另一个

更新时间:2023-11-09 23:26:22

You could make the variable global:

function add_custom_price( $cart_object ) {
    global $newVar;
    foreach ( $cart_object->cart_contents as $key => $value ) {
        $newVar = $value['data']->price;
    }
}

function function_name() {
    global $newVar;
    echo $newVar;
}

Or, if $newVar is already available in the global scope you could do:

function function_name($newVar) {
    echo $newVar;
}

// Add the hook
add_action( 'another_area', 'function_name' );

// Trigger the hook with the $newVar;
do_action('another_area', $newVar);

相关阅读

推荐文章