title: PHP variable scope - How to share variable between two functions without using arguments?
body: <p>I have a small problem in a script I can't solve on my own. Having studied the PHP documentation regarding variable scope, I am not sure if this is even possible.</p>
  
  <p>Take the following example:</p>
  
  <pre><code>function my_funct_a() {
      // Do stuff
      return $a;
  }
  
  function my_funct_b() {
      // Do other stuff
      return $a + $b;
  }
  </code></pre>
  
  <p>You see, the problem is that in <code>my_funct_b</code>, <code>$a</code> is not available because it was declared (and is returned) in <code>my_funct_a</code>.  </p>
  
  <p>Normally I would pass this variable as an argument, but this is not possible at this point due to some kind of framework limitation.</p>
  
  <p>So I tried to do it like this:  </p>
  
  <pre><code>function my_funct_a() {
      // Do stuff
      global $a;
      return $a;
  }
  
  function my_funct_b() {
      // Do other stuff
      global $a;
      return $a + $b;
  }
  </code></pre>
  
  <p>This also didn't work, I think because <code>global</code> works 'the other way around'. Instead of declaring a variable as global inside a function to be available outside the function, it has to be declared as global outside the function to be available inside the function.  </p>
  
  <p>The Problem is that the value of <code>$a</code> is created in <code>my_funct_a</code>, so I can't global it before the value is known.<br>
  Because of that, I tried to do it like this:</p>
  
  <pre><code>// global variable, but no value assigned yet
  global $a
  
  function my_funct_a() {
     // Do stuff
     global $a;
     $a = 1; 
     return $a;
  }
  
  function my_funct_b() {
     // Do other stuff
     global $a;
     return $a + $b;
  }
  </code></pre>
  
  <p>This also didn't work. Why? Is it even possible without passing the variable as an argument?</p>