将一个函数中创建的 PHP 变量传递给另一个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18517705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass PHP variable created in one function to another
提问by JCHASE11
I am using wordpress and woocommerce ( an e-commerce plugin) to customize a shopping cart. Within my functions.php I am storing data in a variable like so:
我正在使用 wordpress 和 woocommerce(一个电子商务插件)来自定义购物车。在我的functions.php中,我将数据存储在一个变量中,如下所示:
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$newVar = $value['data']->price;
}
}
I need to be able to use $newVar
in a different function so I can echo the result on a different area of the page. For instance, if I had the following function, how would I use $newVar
within it?
我需要能够$newVar
在不同的函数中使用,以便我可以在页面的不同区域回显结果。例如,如果我有以下功能,我将如何$newVar
在其中使用?
add_action( 'another_area', 'function_name' );
function function_name() {
echo $newVar;
}
How can I do this?
我怎样才能做到这一点?
回答by putvande
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:
或者,如果$newVar
已经在全局范围内可用,您可以执行以下操作:
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);
回答by ChunkyBaconPlz
Any reason why you can't call your function from within your foreach loop?
为什么不能从 foreach 循环中调用函数?
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$newVar = $value['data']->price;
function_name($newVar);
}
}
回答by Emilio Gort
You should use return $variable in your functions:
您应该在函数中使用 return $variable :
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$newVar = $value['data']->price;
}
return $newVar;
}
function function_name($newVar) {
//do something with $newVar
}
And use like this:
并像这样使用:
$variable = add_custom_price( $cart_object );
$xxx = function_name($variable);
UPDATE:
更新:
Looking at what @ChunkyBaconPlz said $newVar should be an array:
看看@ChunkyBaconPlz 所说的 $newVar 应该是一个数组:
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$newVar[] = $value['data']->price;
}
return $newVar;
}
回答by Link of Origin
This is a scope issue. You need to instantiate (create) $newVar outside of the function first. Then it will be view-able by your other function.
这是一个范围问题。您需要先在函数外部实例化(创建)$newVar。然后它可以被您的其他功能查看。
You see, scope determines what objects can be seen other objects. If you create a variable within a function, it will only be usable from within that function. Once the function ends, that variable is eliminated!
你看,范围决定了哪些对象可以被其他对象看到。如果您在函数内创建变量,则它只能在该函数内使用。一旦函数结束,该变量就被消除了!
So to fix, literally just create "$newVar" before you create the function and you should be good to go.
因此,要修复,实际上只需在创建函数之前创建“$newVar”就可以了。
回答by Rakhi
even if its defined global its not working while calling in other function how ever if we call function in other function it seems to be working.
即使它定义的全局它在调用其他函数时不起作用,但是如果我们在其他函数中调用函数它似乎正在工作。
<?php
function one() {
global $newVar;
$newVar = "hello";
}
function two() {
one();
global $newVar;
echo $newVar;
}
two();
?>