This question already has an answer here:

I creating functions in my function.php and I have a question regarding passing local variables between functions. Let say I have the functions below, A and B, and I have the variable $amount. Now let's say in Function A, $amount changes according to certain conditions. How do I make sure that the $amount used in Function B, is the updated variable and not just the variable I declared at the beginning. I thought of using static or globals but I am not sure how this applies to a variable which is dynamic.

 function A () {
    $amount = 5;

      if ($_POST['term_id'] == '74' )
        $amount = -20;

      elseif ($_POST['term_id'] == '75' )
        $amount = 500;

 function B () {

   echo $amount;


Edit: To toscho, I understand the similarities between the two questions, but my variable is a dynamic variable, whereas the user's variable in the other questions is always constant.

marked as duplicate by fuxia Jul 11 '16 at 1:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @toscho I appreciate you linking me to the other question, but my knowledge of PHP is limited as I am still learning and I prefer not to use a class. Is there any other method that I can use? Furthermore, My variable is a dynamic one and the user in the other questions has a constant variable as far as I can see. – LearntoExcel Jul 11 '16 at 2:01
  • A variable is always changeable, hence the word. :) There is no other way besides global variables, and these are very unsafe, because any other code can overwrite them. – fuxia Jul 11 '16 at 2:58

Browse other questions tagged or ask your own question.