简体   繁体   中英

Variable is already defined but still its not working inside the function

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.

Variable $selected doesn't work. I tried few thinks like

  1. Making the variable global inside the function - Fails
  2. Testing with dummy text - Fails
  3. echo the dummy text outside the condition tag - Fails

More Information

The variables only works when they are being used before this line of code

$indir = array_filter($files, function($item){

When I remove the global $selected I received this error message

Notice:  Undefined variable: selected in 

This is what I current

/* 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 "<select class=\"fs12 template\" id=\"template\" name=\"".$name."\">";
  if($noparent){ echo "<option value=\"-1\">No Parent</option>";}
  $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 "<option value=\"" . $template . $selected . "\" selected=\"selected\">" . $template . "</option>";
          } else {
            echo "<option value=\"" . $template . $selected . "\">" . $template . "</option>";
          }
        }
      }
    }
  });
  echo "</select>";
}

Before function is called:

$selected; // Undefined.

In beginning of function:

$selected = "Works!";

Then you overwrite it

global $selected;

Making it's content being undefined. Don't overwrite the value and dump the content of it to check what it actually is.

See variable scope: http://www.php.net/manual/en/language.variables.scope.php

$selected = 'global';

function get_theme_templates(){
    $selected = 'get_theme_templates';
    $indir = array_filter($files, function($item){
        print_r($selected);
    });
}

PHP does not behave the same as JS. The array_filter block of code will not have $selected in it. If you do a global $selected inside this function, it will get the global value, not the value from inside get_theme_templates().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM