title: Variable is already defined but still its not working inside the function
body: <p>I have function which will scan the directory and get's the php files from the directory,  then same php file it will match the content and get the Template Name. For some weird reason the variables which are passed to the function is not begin recognised.</p>
  
  <p>Variable <strong>$selected</strong> doesn't work. I tried few thinks like</p>
  
  <ol>
  <li>Making the variable global inside the function - Fails</li>
  <li>Testing with dummy text - Fails</li>
  <li>echo the dummy text outside the condition tag - Fails</li>
  </ol>
  
  <p><strong>More Information</strong></p>
  
  <p>The variables only works when they are being used before this line of code</p>
  
  <pre><code>$indir = array_filter($files, function($item){
  </code></pre>
  
  <p>When I remove the <strong>global $selected</strong> I received this error message</p>
  
  <pre><code>Notice:  Undefined variable: selected in 
  </code></pre>
  
  <p><strong>This is what I current</strong></p>
  
  <pre><code>/* Scan's the theme directory and gets the PHP files which has Template named defined (Template: Your Template Name)*/  
  function get_theme_templates($name = "template", $noparent = true, $selected = "Works!"){
    echo "&lt;select class=\"fs12 template\" id=\"template\" name=\"".$name."\"&gt;";
    if($noparent){ echo "&lt;option value=\"-1\"&gt;No Parent&lt;/option&gt;";}
    $dir = dirname(dirname(dirname(__FILE__))) . '/system/themes/';
    $files = scandir($dir);
    $indir = array_filter($files, function($item){
      if($item[0] !== '.'){
        if( get_extension($item) == "php"){
          global $dir;
          $search = "Template: ";
          @header('Content-Type: text/plain');
          $contents = file_get_contents(dirname(dirname(dirname(__FILE__))) . '/system/themes/' . $item);
          $pattern = preg_quote($search, '/');
          $pattern = "/^.*$pattern.*\$/m";
          if(preg_match_all($pattern, $contents, $matches)){
            $template = implode("\n", $matches[0]);
            $template_sanitize = array("Template:", "/* ", " */");
            $template = str_replace($template_sanitize, "", $template);
            $template = trim($template);
            if( trim($template) == trim($selected) ){
              echo "&lt;option value=\"" . $template . $selected . "\" selected=\"selected\"&gt;" . $template . "&lt;/option&gt;";
            } else {
              echo "&lt;option value=\"" . $template . $selected . "\"&gt;" . $template . "&lt;/option&gt;";
            }
          }
        }
      }
    });
    echo "&lt;/select&gt;";
  }
  </code></pre>