Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 475 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass PHP variable created in one function to another

#1
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?
Reply

#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);
}
}
Reply

#3
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.

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.
Reply

#4
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);
Reply

#5
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);

<b>UPDATE:</b>

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;
}
Reply

#6
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();

?>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through