简体   繁体   English

将变量从一个函数传递到另一个函数

[英]Passing variable from one function to another function

I am trying to pass a variable from one function to another. 我试图将变量从一个函数传递到另一个函数。 I have several other functions that require $member_id which is provided in the first function wordpress_user_id(). 我还有其他一些需要$ member_id的函数,这些函数在第一个函数wordpress_user_id()中提供。

function wordpress_user_id() {
    $member_id = get_current_user_id();         
    return $member_id;
}

I need to use $member_id in this function: 我需要在这个函数中使用$ member_id:

function yif_wlmm_get_wp_user_id() {
    $output = 'Your ID is: ' . $member_id;          
    return $output;
}

I tried passing $member_id from the first function like this: 我尝试从第一个函数传递$ member_id,如下所示:

function yif_wlmm_get_wp_user_id($member_ID) 

and

function yif_wlmm_get_wp_user_id( wordpress_user_id() ) 

but neither one works. 但是没有人工作。

yif_wlmm_get_wp_user_id( wordpress_user_id() ) 

does not return $member_id . 不返回$member_id Do I need to use a global? 我需要使用全局吗?

To make that code pass anything, you need to do this: 要使代码通过任何内容,您需要执行以下操作:

$id = wordpress_user_id();
yif_wlmm_get_wp_user_id($id);

or, more concise: 或者,更简洁:

yif_wlmm_get_wp_user_id(wordpress_user_id());

Otherwise the functions have nothing to do with each other, even if you name their parameters identically. 否则,即使您以相同的方式命名参数,这些函数也无法相互关联。

Whether a class is the better use or not is impossible to answer without knowing more about how this is used. 如果一个类是否更好用,如果不了解更多如何使用它,就无法回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将变量值从一个函数传递到另一个函数 - Passing variable value from one function to another 将变量从一个ajax函数传递到另一个ajax函数 - passing variable from one ajax function to another ajax function 将变量从一个函数传递到同一控制器中的另一个函数 - Passing variable from one function to another function in same controller 将变量从一个函数传递到同一控制器中的另一个函数(laravel 5) - Passing variable from one function to another function in same controller (laravel 5) 在Codeigniter中将uri分段变量从一个函数传递给另一个函数 - Passing uri segment variable from one function to another in Codeigniter Codeigniter:将post变量从一个函数传递到控制器中的另一个函数 - Codeigniter: passing a post variable from one function to another in a controller 无法在PHP中将变量从一个函数传递给另一个函数? - Trouble passing a variable from one function to another in PHP? 通过AJAX调用以自动完成方式将变量值从一个函数传递给另一个函数 - Passing variable value from one function to another in autocomplete with AJAX call Laravel - 将变量从 function 传递到另一个 function - Laravel - Passing a variable from a function to another function 将一个函数变量作为参数传递给Class的另一个函数 - passing one function variable as a argument to another function of a Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM