繁体   English   中英

PHP条件是随机输出的吗?

[英]PHP condition is being output randomly?

我的条件陈述之一有问题。 下面的代码是单个文本搜索,用户可以在其中输入字符串并检查目录中的一组文件。 尽管我的输出故障很小,但是代码运行良好。

下面的第二个条件(在find_files函数之前)在我的搜索结果中间显示一个echo语句。 换句话说,尽管第二条条件语句在搜索结果中出现一次,但我的结果仍显示得很好。

更奇怪的是,条件条件确实可以在应有的条件下起作用(即,当我输入一个字符串并且在文件内没有找到字符串“ string”时),所以我感到困惑。 而且我知道条件不包含在循环中,所以为什么在搜索过程中完全显示条件?

这是我需要解决的最后一个故障,它将非常有效。 任何帮助,将不胜感激。

 <?php

 $query = $_POST['query'];



 if ((isset($query)) && (empty($query))) {
 echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
 }


 elseif ((isset($query)) && (!find_files('.')))  { 
 echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>"; 
 } 



 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 style=\"color:darkgray; font-
family:arial\">Could not check $this_file</p>\n"); }
else { if(stristr($content, $str_to_find)) { echo("<p style=\"color:darkgray; font-
family:arial\">$this_file -> contains $str_to_find</p>\n"); }}
unset($content);
}

?>

简单地增加回报将无济于事。 此修改后的代码有效,并且没有显示错误

    if (!isset($_REQUEST['query']))
    {
        //Ask for query here :)     
        //echo "<p style=\"color:darkgray; font-family:arial\">No query specified.</p>";
        exit;
    }

    $query = isset($_REQUEST['query']) ? $_REQUEST['query'] : '';


    if (empty($query))
    {
        echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
        exit;
    }


    $filesFound = find_files('.');

    if (!$filesFound)
    {
        echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
    }


    function find_files($seed)
    {
        if (!is_dir($seed)) return false;
        $found = false;
        $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))
                        {
                            if (!$found)
                            {
                                $found = check_files($path);
                            }
                        }
                    }
                }
                closedir($dh);
            }
        }
        return $found;
    }

    function check_files($this_file)
    {
        $query = $_REQUEST['query'];

        $str_to_find = $query;


        if (($content = file_get_contents($this_file)) === false)
        {
            echo("<p style=\"color:darkgray; font-family:arial\">Could not check $this_file</p>\n");
            return false;
        }
        else
        {
            if (stristr($content, $str_to_find))
            {
                echo("<p style=\"color:darkgray; font-family:arial\">$this_file -> contains $str_to_find</p>\n");
                return true;
            }
        }
    }

在第二个条件中,您要检查条件: !find_files('.')匹配。 为了检查该条件,PHP当时实际上正在运行该函数以获取其返回值,然后它会检查该条件。

最重要的是,当提供错误的输入时, find_files()函数将返回false,但成功时将不发送返回值。 这意味着它不会为条件语句提供一个值为正的值,因此!find_files('.')值为true ,并且echo语句运行。

要解决此问题,您只需添加return true; 作为find_files()函数的最后一行。

但我也建议您修复该函数运行两次的事实。 使用类似:

if ((isset($query)) && (empty($query))) {
    echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
}

else {
    $success = find_files('.');

    if ((isset($query)) && (!$success))  { 
        echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
    }
}

代替:

if ((isset($query)) && (empty($query))) {
    echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
}

elseif ((isset($query)) && (!find_files('.')))  { 
    echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>"; 
}

find_files('.');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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