2014-04-27 5 views
0

디렉토리를 검색하고 디렉토리에서 PHP 파일을 가져 오는 기능이 있습니다. 그러면 동일한 PHP 파일이 내용과 일치하고 템플릿 이름을 가져옵니다. 어떤 이상한 이유 때문에 함수에 전달 된 변수가 인식되기 시작하지 않습니다.변수가 이미 정의되어 있지만 여전히 함수 내에서 작동하지 않습니다.

변수 $이 선택되지 않습니다. 내가 시도 몇

  1. 함수 내부의 변수 글로벌 만들기 같은 생각 - 더미 텍스트
  2. 테스트를 실패 -
  3. 이 조건 태그 외부 더미 텍스트를 에코 실패 -

더 많은 실패 정보

변수는이 코드 줄 앞에 사용되는 경우에만 작동합니다.

0 내가 선택한 글로벌 $ 나는이 오류 메시지가

Notice: Undefined variable: selected in 

을받은 제거하면

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

이 내가

/* 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>"; 
} 

답변

0

현재 함수가 호출되기 전에 무엇을 :

$selected; // Undefined. 
기능의 시작에서 6,

:

$selected = "Works!"; 

그런 다음 당신은 정의되어 있지 않다고 컨텐츠의 제작이

global $selected; 

을 덮어 씁니다. 값을 겹쳐 쓰지 말고 내용을 덤프하지 마십시오. 실제로 내용을 확인하십시오. 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가 JS와 동일하게 동작하지 않는다 :

+0

나는 이것을 시도하고 "Notice : Undefined variable : selected" – user3140617

0

가변 범위를 참조. array_filter 코드 블록에는 $가 선택되지 않습니다. 이 함수 내에서 전역 $을 선택하면 get_theme_templates() 내부의 값이 아닌 전역 값을 가져옵니다.

관련 문제