2012-02-24 62 views
0

您可以從我的代碼中看到我已經設置了一個變量($ query)等於從外部表單發佈的數據。在那之下,我通過迴應它來測試變量,所以變量似乎被正確地建立。PHP中的變量語法

問題是,接近底部我試圖創建另一個名爲$ str_to_find的變量,我希望它設置爲輸出我的原始變量$ query。然而,當我查看輸出時,代碼在我的代碼底部附近處理這個變量後什麼也沒有顯示出來。我不明白爲什麼它不顯示輸出。

<?php 
$query = $_POST['query']; 

echo "$query"; 

find_files('.'); 
function find_files($seed) { 
    if(! is_dir($seed)) return false; 
    $files = array(); 
    $dirs = array($seed); 
    while(NULL !== ($dir = array_pop($dirs))) 
    { 
     if($dh = opendir($dir)) 
     { 
      while(false !== ($file = readdir($dh))) 
      { 
       if($file == '.' || $file == '..') continue; 
       $path = $dir . '/' . $file; 
       if(is_dir($path)) { 
        $dirs[] = $path; 
       } 
       else { 
        if(preg_match('/^.*\.(php[\d]?|js|txt)$/i', $path)) { 
         check_files($path); 
        } 
       } 
      } 
      closedir($dh); 
     } 
    } 
} 

function check_files($this_file) { 
    $str_to_find = $query; 
    if(!($content = file_get_contents($this_file))) { 
     echo("<p>Could not check $this_file</p>\n"); 
    } 
    else { 
     if(stristr($content, $str_to_find)) { 
      echo("<p>$this_file -> contains $str_to_find</p>\n"); 
     } 
    } 
    unset($content); 
} 
?> 

更新的代碼

<?php 
$query = $_POST['query']; 



find_files('.'); 
function find_files($seed) 

{ 


if(! is_dir($seed)) return false; 
$files = array(); 
$dirs = array($seed); 
while(NULL !== ($dir = array_pop($dirs))) 
{ 
    if($dh = opendir($dir)) 
    { 
     while(false !== ($file = readdir($dh))) 
     { 
      if($file == '.' || $file == '..') continue; 
      $path = $dir . '/' . $file; 
      if(is_dir($path)) { $dirs[] = $path; } 
      else { if(preg_match('/^.*\.(php[\d]?|js|txt)$/i', $path)) { check_files($path); }} 
     } 
     closedir($dh); 
    } 
} 
} 

function check_files($this_file) 
{ 

$query = $_POST['query']; 

$str_to_find = $query; 
if(!($content = file_get_contents($this_file))) { echo("<p>Could not check $this_file</p>\n"); } 
else { if(stristr($content, $str_to_find)) { echo("<p>$this_file -> contains 
$str_to_find</p>\n"); }} 
unset($content); 
} 

?> 
+1

HTTP: //php.net/manual/en/language.variables.scope.php $ query是在函數check_files()之外定義的,所以它不存在於那裏:或者使其成爲'global',或者更好的是將它作爲參數函數 – 2012-02-24 20:51:07

+1

'function check_files($ this_file){global $ query; $ str_to_find = $ query;' – Cheery 2012-02-24 20:52:22

回答

3

這是作用域的問題。您的$查詢變量(實際上並沒有直接在函數體內實例化的變量)在check_files中不可用。

您應該將$ query作爲參數傳遞給函數。

function check_files($this_file, $query) { 
    // ... 
} 

另一種選擇是使變量'全局',但這很少是一個明智的想法。

+0

打敗我吧,幹得好。 – 2012-02-24 20:54:41

+1

+1,但不適用於全局建議。如果你必須使用'global',你很可能做錯了。 – simshaun 2012-02-24 20:56:23

+0

@simshaun我建議它不是一個好主意,但它仍然是一個有效的選擇,我認爲在變量範圍方面需要注意。 – jstephenson 2012-02-24 20:57:20

0

變量$query被聲明在函數check_files()範圍之外。如果你想訪問它,把global $query;放在函數的開頭。

2

它不起作用的原因是因爲$ query超出了函數的範圍。如果你想使用它的內部函數外聲明的變量,你要麼需要通過通過它作爲一個參數,或使用

function check_files($this_file) { 
    global $query; 
    $str_to_find = $query; 

雖然將其通過作爲參數最好使用全局。

0

您需要聲明的$query全球爲每PHP manual,如果不是,解析器會認爲$query是局部範圍的變量(local爲「只在此功能」中)

function check_files($this_file) { 
global $query; 
$str_to_find = $query; 
...