Pass PHP variable created in one function to another
Solution 1
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);
Solution 2
Any reason why you can't call your function from within your foreach loop?
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$newVar = $value['data']->price;
function_name($newVar);
}
}
Solution 3
You should use return $variable in your functions:
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:
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$newVar[] = $value['data']->price;
}
return $newVar;
}
Solution 4
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();
?>
JCHASE11
Updated on July 09, 2022Comments
-
JCHASE11 almost 2 years
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:
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?add_action( 'another_area', 'function_name' ); function function_name() { echo $newVar; }
How can I do this?
-
ChunkyBaconPlz over 10 yearsThis is assuming he wants to set
$newVar
multiple times, and then return only the last iteration of$newVar
set by hisforeach
loop. -
putvande over 10 yearsYou still need to make the var
global
. -
JCHASE11 over 10 yearsdoes global $newVar need to be defined in the new function?
-
Emilio Gort over 10 yearsa simple question...why declare $newVar global, is
global
a bad practice?? and should be $newVar an array?is a really question to clear up my ideas -
putvande over 10 years@JCHASE11 yes
global $newVar
needs to be defined in both functions. No it is not a bad practice, it is necessary to get the variables in the right scope. You could also use superglobals php.net/manual/en/language.variables.superglobals.php. -
Link of Origin over 10 yearsOh really? Huh, I didn't know that. Thank you, I will keep that in mind!