[英]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.