您可以從我的代碼中看到我已經設置了一個變量($ 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);
}
?>
HTTP: //php.net/manual/en/language.variables.scope.php $ query是在函數check_files()之外定義的,所以它不存在於那裏:或者使其成爲'global',或者更好的是將它作爲參數函數 – 2012-02-24 20:51:07
'function check_files($ this_file){global $ query; $ str_to_find = $ query;' – Cheery 2012-02-24 20:52:22