title: Loading PHPs via XMLHTTPRequest don't share the same variable scope
body: <p>I understand the concept of global variables in PHP and understand the pros/cons regarding the use of global variables. Nevertheless, I've decided to use them, but I'm running into issues regarding the scope and visibility of them.</p>
  
  <p>Situation:</p>
  
  <p>Depending on the selection of a menu I'm loading different PHPs into a div. The PHPs require the same common set of data, which I want to avoid to be reloaded and kept in memory all the time for each PHP. In the example below the <code>GlobalVars.php</code> will be kept in memory twice and will also fetch the data from the database twice. </p>
  
  <p>The problem is, that by loading them into the div, they don't share the scope of the <code>main.html</code>. The global variables in <code>GlobalVars.php</code> can be seen and accessed by the code in <code>another.php</code>, but not in <code>PHP1.php</code>, nor in <code>PHP2.php</code>.</p>
  
  <p>GlobalVars.php:</p>
  
  <pre><code>&lt;?php
      $var1 = "*";
      $var2 = 5;
      // Various SQL fetches
  ?&gt;
  </code></pre>
  
  <p>Main.html:</p>
  
  <pre><code>&lt;?php require_once="./GlobalVars.php"; ?&gt;
  &lt;?php require_once="./another.php"; ?&gt;
  
  &lt;script&gt;
      function LoadHTML(href) {
          var xmlhttp = new XMLHttpRequest();
          xmlhttp.open("GET", href, false);
          xmlhttp.send();
          return xmlhttp.responseText;   
      }
  
      switch(menuitem) {
          case 0: break;
          case 1: document.getElementById("contentdiv").innerHTML=LoadHTML("./PHP1.php") break;
          case 2: document.getElementById("contentdiv").innerHTML=LoadHTML("./PHP2.php") break; break;
          case 3: break;
          default:
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>PHP1.html:</p>
  
  <pre><code>&lt;?php
      require_once="./GlobalVars.php";
      // code ...
  ?&gt;
  </code></pre>
  
  <p>PHP2.html:</p>
  
  <pre><code>&lt;?php
      require_once="./GlobalVars.php";
      // code ...
  ?&gt;
  </code></pre>
  
  <p>The question is, how can I load the PHPs into a div and 'see' and work with the variables in the scope of <code>main.html</code>?</p>
  
  <p>Regards</p>
  
  <p>Carsten</p>