title: "Global" variable scope in PHP
body: <p>I have a section of code like the following:</p>
  
  <p>---- file.php ----</p>
  
  <pre><code>require_once("mylib.php");
  
  function($a,$b)
  {
      $r = $_GLOBALS['someGlobal'];
      echo $r;
  }
  </code></pre>
  
  <hr>
  
  <p>---- mylib.php ----</p>
  
  <pre><code>$_GLOBALS['someGlobal'] = "Random String";
  </code></pre>
  
  <hr>
  
  <p>This is a bit trivialized, but is the exact problem I have an I haven't found some related stuff, but nothing that answers my question directly.</p>
  
  <p>When I call <code>function($a,$b)</code> nothing is echo'd, that is - <code>$r</code> is "empty" as if nothing was ever assigned to <code>$_GLOBALS['someGlobal']</code>;</p>
  
  <p>In addition, I have tried with the following:</p>
  
  <pre><code>global $someGlobal;
  $someGlobal = "Random String";
  </code></pre>
  
  <p>Same thing, no effect.
  Also, in <code>file.php</code> if I try with <code>global</code>, or with just <code>$someGlobal</code> it still does not work.</p>
  
  <p>As far as I know, from the documentation on php.net using global <code>$someGlobal</code> in mylib.php (and having that inserted in the top level of file.php) that it would not actually do much since it's already at the "top-level" of the scope hierarchy as far as I can tell. However, I thought registering it as a global might allow it to be accessed from inside the function, but this is clearly not the case.</p>
  
  <p>Can anybody explain why, and explain how to get around this?</p>
  
  <p>Edit: I should not that in file.php if I use <code>$_GLOBALS['someGlobal'];</code> the value is recovered fine if it's not in a function.</p>