如何通过php函数显示错误消息


How to display error messages via function php

目标是让foreach循环的结果在表单页面中显示数组的内容。我已经能够显示错误消息,但当存在多个错误消息时,不会超过一个。目前,代码返回的集合数组有很多行,但我的目的是通过各自的条件逐行生成。

index.php

<tbody>
            <?php
include './function/check.php';
            foreach ($urls as $url) {
                ?>

                <tr>
                    <td><a href="detail.php"> <?php print_r($url[2]); ?></a></td>
                    <td><a href="detail.php?pno=<?php print_r($url[1]); ?>"><?php print_r($url[1]); ?></a></td>

                    <td> 
                        <?php
                            var_dump($output);
                        ?>
                    </td>
                </tr>
                        <?php
                    }
                }
                ?>
    </tbody>

check.php

<?php

  include 'checkfunction.php';

  if (isset($_POST['sourceDir'])) {
  $sourceDir = $_POST['sourceDir'];
  $urls = array();
  $linecount = 1;
  $output = array();
  $errmsg = '';
  if (is_dir($sourceDir)) {
    if ($dh = opendir($sourceDir)) {
        while (false !== ($filename = readdir($dh))) {
            if ($filename != "." && $filename != ".." && strtolower(substr($filename, strrpos($filename, '.') + 1)) == 'php') {
                $absolute_path = str_replace('''', '/', $sourceDir) . '/';
                $readfile = fopen($absolute_path . $filename, 'r');
                if ($readfile) {
                    while (FALSE !== ($line = fgets($readfile))) {
                        $ary = check($line, $linecount, $filename);
                        if ($ary != null) {
                            array_push($urls, $ary);
                            $errmsg = array($errmsg);
                            $out1 = '';
                            foreach ($errmsg as $k => $v) {
                                $out1 = $v;
                                $output[] = $out1;
                            }

                            var_dump($output);

                        }
                        $linecount++;
                    }
                    fclose($readfile);
                }
                $linecount = 1;
            }
        }
        closedir($dh);
    }
}
}
?>

checkfunction.php

<?php
function check($line, $linecount, $filename) {
global $errmsg;
$urls = array($line, $linecount, $filename);
if (preg_match("/'=/", $line)) {
    if (preg_match("/''s'=''s/i", $line) || preg_match("/''s'==''s/", $line) || preg_match("/''s'===''s/", $line)) {
    } else {
        $errmsg = "There should be space before and after of equal";
        return $urls;
    }
} elseif (strpos($line, 'if') !== false) {
    if (preg_match("/'!'''$[a-zA-z0-9]/i", $line)) {
        $errmsg = "•If you want to use ifcondition, don't use !.Use false === ";
        return $urls;
    }
}
}

只需在第一页打开后添加ini_set('display_errors',1);

这相当于在php.ini 中启用error_reporting

//我希望这就是你想要的。。

要做到这一点,您需要使用foreach循环,请尝试以下操作:

$errors = array(); //consider this is the array where ur storing all the errors, im doing this to make it more logical  
//for each item in the array store it in a new variable named $error, and then we echo it :
foreach($errors as $error){
      //you can also style all the errors, or make them a part of a <ul> like this
      echo '<ul>
            <li>'.$error.'</li>
            </ul>';
}

这应该显示您拥有的所有错误消息