title: PHP says variable doesn't exist - even though it does
body: <p>The following code gives me the following error, even thought the variable 'cache_path' has been defined at the top.</p>
  
  <pre><code>&lt;b&gt;Notice&lt;/b&gt;:  Undefined variable: cache_path in &lt;b&gt;C:\Users\Jan Gieseler\Desktop\janBSite\Scripts\Index.php&lt;/b&gt; on line &lt;b&gt;20&lt;/b&gt;&lt;br /&gt;
  </code></pre>
  
  <p>Here is the code;
      
  
  <pre><code>header('Content-type: application/x-javascript');
  
  $cache_path = 'cache.txt';
  
  function getScriptsInDirectory(){
      $array = Array();
      $scripts_in_directory = scandir('.');
      foreach ($scripts_in_directory as $script_name) {
          if (preg_match('/(.+)\.js/', $script_name))
          {
              array_push($array, $script_name);
          }
      }
      return $array;
  }
  
  function compilingRequired(){
      if (file_exists($cache_path))
      {
          $cache_time = filemtime($cache_path);
          $files = getScriptsInDirectory();
          foreach ($files as $script_name) {
              if(filemtime($script_name) &gt; $cache_time)
              {
                  return true;
              }
          }
          return false;
      }
      return true;
  }
  
  if (compilingRequired())
  {
  }
  else
  {
  }
  
  ?&gt;
  </code></pre>
  
  <p>What could I do to fix this?</p>
  
  <p>EDIT: I've thought that PHP makes variables which are in the 'main' scope available for functions, too. I guess, I was wrong. Thanks for the help.</p>
  
  <p>I've fixed it by using the 'global' statement.</p>