Php 变量已定义,但在函数中仍不起作用

Php 变量已定义,但在函数中仍不起作用,php,templates,Php,Templates,我有一个函数,它将扫描目录并从目录中获取php文件,然后相同的php文件将匹配内容并获取模板名称。由于某种奇怪的原因,传递给函数的变量无法识别 变量$selected不起作用。我试过几次 将变量设置为函数内的全局变量-失败 使用虚拟文本进行测试-失败 回显条件标记外部的伪文本-失败 更多信息 变量只有在这行代码之前使用时才起作用 $indir = array_filter($files, function($item){ 当我删除选中的全局$selected时,我收到了此错误消息 Notice

我有一个函数,它将扫描目录并从目录中获取php文件,然后相同的php文件将匹配内容并获取模板名称。由于某种奇怪的原因,传递给函数的变量无法识别

变量$selected不起作用。我试过几次

  • 将变量设置为函数内的全局变量-失败
  • 使用虚拟文本进行测试-失败
  • 回显条件标记外部的伪文本-失败
  • 更多信息

    变量只有在这行代码之前使用时才起作用

    $indir = array_filter($files, function($item){
    
    当我删除选中的全局$selected时,我收到了此错误消息

    Notice:  Undefined variable: selected in 
    
    这就是我目前的想法

    /* 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>";
    }
    
    /*扫描主题目录并获取已定义模板的PHP文件(模板:您的模板名称)*/
    函数获取主题模板($name=“template”,$noparent=true,$selected=“Works!”){
    回声“;
    如果($noparent){echo“No Parent”;}
    $dir=dirname(dirname(dirname(_文件__)))。/system/themes/;
    $files=scandir($dir);
    $indir=array\u filter($files,function($item){
    如果($item[0]!=='。){
    if(获取扩展名($item)=“php”){
    全球$dir;
    $search=“模板:”;
    @标题(“内容类型:文本/普通”);
    $contents=file_get_contents(dirname(dirname(dirname(u file_u)))。/system/themes/'.$item);
    $pattern=preg_quote($search,“/”);
    $pattern=“/^.*$pattern.*\$/m”;
    if(preg_match_all($pattern、$contents、$matches)){
    $template=内爆(“\n”,$matches[0]);
    $template_sanitize=数组(“模板:”、“/*”、“*/”;
    $template=str\u replace($template\u sanitize,“,$template);
    $template=修剪($template);
    如果(修剪($template)=修剪($selected)){
    回显“$template.”;
    }否则{
    回显“$template.”;
    }
    }
    }
    }
    });
    回声“;
    }
    
    在调用函数之前:

    $selected; // Undefined.
    
    在功能开始时:

    $selected = "Works!";
    
    然后覆盖它

    global $selected;
    
    使其内容未定义。不要覆盖该值并转储其内容以检查其实际内容。

    请参阅变量范围:

    PHP的行为与JS不同。代码的数组\过滤器块中不会选择$。如果在该函数中选择全局$,它将获取全局值,而不是get_theme_templates()中的值

    我尝试了这个,收到了“通知:未定义变量:已选择”
    $selected = 'global';
    
    function get_theme_templates(){
        $selected = 'get_theme_templates';
        $indir = array_filter($files, function($item){
            print_r($selected);
        });
    }